在其中一个 json 响应中,我以字符串格式“2020-12-30T02:07:08.500254-05:00”接收到这样的日期。现在我的要求就像我需要根据日期对 json 进行排序。无法将上述字符串转换为日期格式以使用排序功能。
需要帮助对此日期格式进行排序。
[
{
"id":1,
"start":"2019-12-30T02:07:08.500254-05:00",
"subject":"test1",
},
{
"id":2,
"start":"2020-12-30T02:07:08.500254-05:00",
"subject":"test2",
},
{
"id":3,
"start":"2018-12-30T02:07:08.500254-05:00",
"subject":"test3",
}
]
我试图对数据进行排序的代码是
db.sort(function(a,b){return new Date(a.start).getTime() - new Date(b.start).getTime();});
答案 0 :(得分:0)
我终于能够自己修复它了。这是代码
function db_sort(a, b){
var a = Date.parse(a.start.substring(0,23) + a.start.substring(26));
var b = Date.parse(b.start.substring(0,23) + b.start.substring(26));
return new Date(b).getTime() - new Date(a).getTime();
}