JSON 日期格式 mm/dd/yyyy
这是我得到的数据:
jsonp1290537545248( [{"Active":true,"EndDate":"\/Date(-62135578800000-0500)\/","StartDate":"\/Date(1280635200000-0400) \/"}]);
$.getJSON(url, {},
function (data) {
alert(data[0].EndDate);
alert(Date(data[0].StartDate));
//alert(data[0].StartDate.getDate());// + "/" + (data[0].StartDate.getMonth() + 1) + "/" + data[0].StartDate.getFullYear()); // alerts: "15/10/2008"
//var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
alert('dd ' + new Date(parseInt(data.substr(6))));
});
如何获取 MM/DD/YYYY 格式?
Here is what I am getting as data:
jsonp1290537545248( [{"Active":true,"EndDate":"\/Date(-62135578800000-0500)\/","StartDate":"\/Date(1280635200000-0400)\/"}] );
$.getJSON(url, {},
function (data) {
alert(data[0].EndDate);
alert(Date(data[0].StartDate));
//alert(data[0].StartDate.getDate());// + "/" + (data[0].StartDate.getMonth() + 1) + "/" + data[0].StartDate.getFullYear()); // alerts: "15/10/2008"
//var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
alert('dd ' + new Date(parseInt(data.substr(6))));
});
How do I get in the MM/DD/YYYY format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会使用与 Zain 发布的类似的正则表达式,但不会像这样使用
eval()
(演示):结束日期是哪一天?这似乎没有什么区别,如果您使用该数字作为新日期,您最终会得到“Sun Dec 31 0000 22:59:59 GMT-0600(中部标准时间)”...所以我不是确定如何处理该值。
I would use a similar regex to what Zain posted, but not use
eval()
like this (demo):And what is that end date? It doesn't seem to be a difference and if you use that number as a new date you end up with "Sun Dec 31 0000 22:59:59 GMT-0600 (Central Standard Time)"... so I wasn't sure what to do with that value.
在确定日期时有必要考虑时区。我假设日期的第一部分是 Java 或 JavaScript 的
Date.getTime()
的输出(即自 1970 年 1 月 1 日 00 点以来的毫秒数: 00:00 世界标准时间)。为了正确输出日期上的所有时间,有必要在创建 Date 对象之前应用时区偏移量(例如
-0500
表示东部标准时间),然后使用UTC 方法来获取部分日期。原因是 JavaScript 没有提供Date.setTimezoneOffset()
方法来将时区设置为正确的时区(无法从访问者的系统时区更改它)。代码示例
这是我想出的代码。它使用正则表达式提取编码日期的各个部分,应用指定的时区偏移量,创建一个 Date 对象,然后从这些部分构建一个日期(演示:http://jsfiddle.net/Wa8LY/1/)。
左填充组件
如果您需要左填充日期的组件(例如
01/01/0001
),您可以使用此函数来帮助执行此操作:并更改最后几行(演示:http://jsfiddle.net/5tkpV/1/):
It's necessary to consider the timezone when determining which date it is. I assume that the first part of the date is the output from
Date.getTime()
of Java or JavaScript (i.e. the number of milliseconds since January 1, 1970, 00:00:00 UTC).For the correct output for all times on a date, it is necessary to apply the timezone offset (e.g.
-0500
for Eastern Standard Time) before creating the Date object and then use the UTC methods to get parts of the date. The reason is that JavaScript does not provide aDate.setTimezoneOffset()
method to set the timezone to the correct one (it's not possible to change it from the visitor's system timezone).Code example
Here's the code I came up with. It uses a regex to extract the parts of the encoded date, applies the specified timezone offset, creates a Date object, and then builds a date from the parts (demo: http://jsfiddle.net/Wa8LY/1/).
Left padding the components
If you need to left pad the components of the date (e.g.
01/01/0001
), you could use this function to help do so:And change the last lines to (demo: http://jsfiddle.net/5tkpV/1/):
这可能会有所帮助。请参阅 http://jsfiddle.net/zainshaikh/pysAR/ 上的演示。
然后您可以使用 JavaScript 日期格式 脚本(缩小后为 1.2 KB) gzipped)以按您想要的方式显示它。
如何设置 Microsoft JSON 日期的格式?
This might help. See the demo at http://jsfiddle.net/zainshaikh/pysAR/.
And then you can use the JavaScript Date Format script (1.2 KB when minified and gzipped) to display it as you want.
How do I format a Microsoft JSON date?
自动将序列化 JSON 日期转换为实际 Javascript 日期
由于您使用的是 jQuery,您可能对我编写的自动将序列化日期转换为实际 Javascript 日期的代码感兴趣。
您的代码仍将在客户端上使用
$.parseJSON()
,但使用第二个参数告诉它自动转换日期。现有代码仍然有效,因为扩展功能仅根据您的需求解析日期。查看博客文章并了解自己。它是可重复使用的,并且可以在全球范围内使用,因此您可以忘记这种手动日期转换。
Auto convert serialized JSON dates to actual Javascript dates
Since you're using jQuery, you might be interested in the code I've written that auto converts serialized dates to actual Javascript dates.
Your code would still use
$.parseJSON()
on the client but with the second parameter where you tell it to automatically convert dates. Existing code will still work, because extended functionality only parses dates on your demand.Check blog post and find out yourself. It's reusable and will work globally so you could just forget about this manual date conversion.
以下内容有效,因为我的日期字符串是“/Date(1334514600000)\”
The following worked because my datestring was "/Date(1334514600000)\"