如何从json日期取得时间? [英] How to get the time elapsed from a json date?

查看:177
本文介绍了如何从json日期取得时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using jquery ajax($.ajax) method to get a json object from c# webmethod.This json object contains a datetime value, which looks something like this '/Date(1329324492302)/' and I tried to parse it using the following js

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

像这样

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

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

I want to find the time elapsed from this datetime to current time in javascript but I am not sure how to do it,as the parsed date looks like a string instead of a date.

有人可以帮忙。

谢谢

推荐答案

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 );

这篇关于如何从json日期取得时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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