如何让返回Json(日期时间)根据UTC返回毫秒

How to let the return Json(datetime) return millisecondes according to the UTC?

本文关键字:返回 根据 UTC 时间 日期 Json      更新时间:2023-09-26

我有一个通过ajax检索事件日期(json格式)的函数。我的函数应该将日期转换为人性化的格式。一切正常,但并不完美。问题是:服务器日期为"21/06/2013 22h00"此函数返回"22/06/2013 05h00"26/07/2013 18h30"此函数返回"27/07/2013 01h30"

这是提前6小时。

附言:在我的国家和我的服务器所在的国家之间,正好有6个小时的差距。。

我必须把UTC函数放在哪里?我的函数到底出了什么问题?谢谢

这是代码:

var jsonifyMyDate = function (jsonDate) {
           var parser = parseInt(jsonDate.substr(6));
           if (parser > 0 && !isNaN(parser)) {
               var newDate = new Date(parser),
               _date = newDate.getDate(),
               _month = newDate.getMonth() + 1,
               _year = newDate.getFullYear(),
               _hour = newDate.getHours(),
               _minute = newDate.getMinutes();
               var dateStr = (_date < 9 ? "0" : "") + _date;
               dateStr += "/" + (_month < 9 ? "0" : "") + _month;
               dateStr += "/" + _year;
               dateStr += " "+(_hour < 9 ? "0" : "") + _hour + "h";
               dateStr += (_minute < 9 ? "0" : "") + _minute;
               /* + "-" + newDate.getSeconds() + "-" + newDate.getMilliseconds() + "";*/
               return dateStr;
           } else return "";

更新:我可以在ActionResult中看到服务器端解析函数的问题。。。所以,当我使用Asp.Net+MVC(C#)时,如何让

return Json(datetime);

是否返回UTC毫秒而不是服务器的UTC毫秒?

Json甲酸酯是基于UTC日期时间创建的。

从ajax调用获得日期时间后,您必须将此UTC日期时间转换为本地日期时区。

例如:

var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"