将json结果转换为日期 [英] Converting json results to a date

查看:114
本文介绍了将json结果转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何格式化JSON日期?

I从JavaScript的$ getJSON调用得到以下结果。如何在JavaScript中将start属性转换为正确的日期?

I have the following result from a $getJSON call from JavaScript. How do I convert the start property to a proper date in JavaScript?


[
{id:1,start :/日期(1238540400000)/},
{id:2,开始:/日期(1238626800000)/}
]

[ {"id":1,"start":"/Date(1238540400000)/"}, {"id":2,"start":"/Date(1238626800000)/"} ]

谢谢!

推荐答案

您需要从字符串中提取数字,并将其传递给Date 构造函数

You need to extract the number from the string, and pass it into the Date constructor:

var x = [{
    "id": 1,
    "start": "\/Date(1238540400000)\/"
}, {
    "id": 2,
    "start": "\/Date(1238626800000)\/"
}];

var myDate = new Date(x[0].start.match(/\d+/)[0] * 1);

部分是:

x[0].start                                - get the string from the JSON
x[0].start.match(/\d+/)[0]                - extract the numeric part
x[0].start.match(/\d+/)[0] * 1            - convert it to a numeric type
new Date(x[0].start.match(/\d+/)[0] * 1)) - Create a date object

这篇关于将json结果转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆