gpt4 book ai didi

javascript - 将json结果转换为日期

转载 作者:IT老高 更新时间:2023-10-28 12:43:10 26 4
gpt4 key购买 nike

Possible Duplicate:
How to format a JSON date?

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

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

谢谢!

最佳答案

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

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

关于javascript - 将json结果转换为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1244094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com