简体   繁体   English

使用Angular js获取系统日期格式

[英]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. 不是完全证明的解决方案,但可以作为参考使用,您可以添加对新格式的支持。

Logic 逻辑

  • Create a date object for a specific date that will exist for every year. 为每年存在的特定日期创建日期对象。 Like: 31/12/2016 喜欢:2016年12月31日
  • Now search first non alphanumeric character. 现在搜索第一个非字母数字字符。 This is your delimiter. 这是你的分隔符。
  • Now that you have 3 parts, you can find dd easily by comparing value to 31 . 既然你有3个部分,你可以通过比较3131来轻松找到dd
  • Even year part is easy, since it can have 2 values, 16 or 2016 . 即使是年份也很容易,因为它可以有2个值, 162016
  • Month can be tricky. 月份可能很棘手。 It can have 12 or Dec or December . 它可以有12DecDecember For that you can have 2 checks. 为此,您可以进行2次检查。 If number and equal to 12, it mm . 如果数字等于12,则为mm If not number and length is 3, its mmm else mmmm 如果不是数字和长度是3,则其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());
})()

Edit 1 编辑1

As correctly pointed out by @ RobG , date.toLocaleString or date.toLocaleDateString are not uniform across all browsers. 正如@ RobG正确指出的那样, date.toLocaleStringdate.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM