如何获取json日期所经过的时间

How to get the time elapsed from a json date?

本文关键字:日期 经过 时间 json 何获取 获取      更新时间:2023-09-26

我使用jquery ajax($.ajax)方法从c#webmethod中获取json对象。这个json对象包含一个datetime值,它看起来像"/Date(1329324492302)/",我试图使用下面的js 解析它

new Date(parseInt(json.d.Date.replace("/Date(", "").replace(")/",""), 10))

结果的日期看起来像这个

Date {Wed Feb 15 2012 16:48:12 GMT+0000 (GMT Standard Time)}

我想在javascript中找到从这个日期时间到当前时间的时间,但我不知道该怎么做,因为解析的日期看起来像字符串而不是日期。

有人能帮忙吗。

感谢

var json = {d:{Date:"/Date(1329324492302)/"}};
var date1 = new Date(parseInt(json.d.Date.replace("/Date(", "").replace(")/",""), 10));
var date2 = new Date();
var diff = (date2-date1);  // 
var hours = Math.floor( diff / 3600000);
var minutes = Math.floor( (diff % 3600000) / 60000);
var seconds = Math.floor( ((diff % 3600000) % 60000) / 1000);
console.log(" elapsed " + hours + ":" + minutes + ":" + seconds );

你可以做:

//timestamp from json
var startTime = parseInt(json.d.Date.replace("/Date(", "").replace(")/",""), 10))
//current timestamp
var endTime = new Date().getTime();
//difference in milliseconds
var diff = endTime - startTime;