[英]Get system date format using Angular js
i want to get system date format using Angular js. 我想使用Angular js获取系统日期格式。 User may have selected it as dd/mm/YYYY, dd/mm/yy ... and so on it can be any date format which is selected by user on his/her computer.
用户可能选择它为dd / mm / YYYY,dd / mm / yy ......等等,它可以是用户在他/她的计算机上选择的任何日期格式。 Is it possible to get the date format?
是否有可能获得日期格式?
--EDIT-- - 编辑 -
I don't have the value of date like 10/12/2016, 10-Dec-2016... and so on this date value also i need to pick from user's system with same format as user have selected. 我没有像12/12/2016,2016年12月10日这样的日期值...等等这个日期值我也需要从用户的系统中选择与用户选择的格式相同的格式。
I think that you can't find out his/her computer date format with only Javascript. 我认为你只能使用Javascript找不到他/她的计算机日期格式。 If you are using some dynamic language on backend, you should try to retrieve time format that way.
如果您在后端使用某种动态语言,则应尝试以这种方式检索时间格式。
I think you can check Mozilla MDN, but I believe JS can't get you this info, because JS is dependent on browser API. 我想你可以查看Mozilla MDN,但我相信JS无法获得这些信息,因为JS依赖于浏览器API。 So, if a browser API provides that then it's fine otherwise use any framework like .Net or Java etc.
因此,如果浏览器API提供了那么它就可以了,否则使用任何框架,如.Net或Java等。
Not a full-proof solution but can but use as a reference and you can add support for new formats. 不是完全证明的解决方案,但可以作为参考使用,您可以添加对新格式的支持。
dd
easily by comparing value to 31
. 31
到31
来轻松找到dd
。 16
or 2016
. 16
或2016
。 12
or Dec
or December
. 12
或Dec
或December
。 For that you can have 2 checks. mm
. mm
。 If not number and length is 3, its mmm
else mmmm
mmm
mmmm
Now you can create var d = new Date('2016/12/31')
and pass d.toLocaleDateString()
to below function. 现在你可以创建
var d = new Date('2016/12/31')
并将d.toLocaleDateString()
传递给下面的函数。
function getDateFormat(dateStr) { var delimiter = dateStr.match(/[^0-9a-z]/i)[0]; var dateParts = dateStr.split(delimiter); var r = dateParts.map(function(d, i) { if (d == 31) { return "dd" } else if (!isNaN(d) && (d == 2016 || (d + 2000) == 2016)) { return d.toString().length === 4 ? "YYYY" : "yy" } else if (d == 12) { return "mm" } else if (isNaN(d)) { var _t = d.replace(/[^az]/gi, '') return _t.length === 3 ? "mmm" : "mmmm" } }); return r.join(delimiter); } Date.prototype.getSystemDateFormat = function(){ return getDateFormat(new Date('2016/12/31').toLocaleDateString()); } //or Date.prototype.SYSTEM_FORMAT = (function(){ return getDateFormat(new Date('2016/12/31').toLocaleDateString()); })() console.log(new Date().getSystemDateFormat()) console.log(new Date().SYSTEM_FORMAT) var d1 = "12/31/2016" console.log(getDateFormat(d1)) d1 = "31/12/2016" console.log(getDateFormat(d1)) d1 = "31 December 2016" console.log(getDateFormat(d1)) d1 = "31 Dec. 2016" console.log(getDateFormat(d1)) d1 = "31-12-2016" console.log(getDateFormat(d1)) d1 = "31.12.2016" console.log(getDateFormat(d1))
You can also add functions to date.prototype to get value directly instead of calling it every time, 您还可以向date.prototype添加函数以直接获取值,而不是每次都调用它,
Date.prototype.getSystemDateFormat = function(){
return getDateFormat(new Date('2016/12/31').toLocaleDateString());
}
//or
Date.prototype.SYSTEM_FORMAT = (function(){
return getDateFormat(new Date('2016/12/31').toLocaleDateString());
})()
As correctly pointed out by @ RobG , date.toLocaleString or date.toLocaleDateString are not uniform across all browsers. 正如@ RobG正确指出的那样, date.toLocaleString或date.toLocaleDateString在所有浏览器中都不一致 。 They are not supported by < IE11.
<IE11不支持它们。 So this is not a reliable answer.
所以这不是一个可靠的答案。 You can use it for reference though.
您可以使用它作为参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.