toJSON()在Date对象上将日期转换回UTC

时间:2022-05-17 02:02:37

I have a date object like below:

我有一个如下的日期对象:

Sun Mar 15 2015 02:30:00 GMT+0530 (IST) [epoch = 1426366800000] 

I need to convert this to a Date JSON format for which I found the function toJSON() on the Date object. But this function is converting the time back to UTC time which is causing a problem for our back end. Any suggestions how to fix this.. unable to find any documentation anywhere.

我需要将其转换为Date JSON格式,我在Date对象上找到了函数toJSON()。但是这个功能正在将时间转换回UTC时间,这对我们的后端造成了问题。任何建议如何解决这个..无法在任何地方找到任何文件。

This is what I am currently getting from toJSON():

这是我目前从toJSON()得到的:

2015-03-14T21:00:00.000Z

2 个解决方案

#1


0  

Use

 var date = "2015-03-14T21:00:00.000Z";
 //toLocaleString will output: 15-‎3‎-‎2015‎ 02‎:30‎:‎00
 var convert = new Date(date).toLocaleString(undefined, {second: "2-digit", minute: "2-digit", hour: "2-digit",  year: 'numeric', day: "2-digit", month: "2-digit"}).replace(/\s/, "T").replace(/(\d{2})-(\d{2})-(\d{4})/, "$3:$2:$1"){4})/, "$3")//will parse it to your local time

 //will output: "‎‎2015-03-15‎T02‎:30‎:‎00"

Replacing the space with a T will make it JSON proof. The Z in the JSON string means convert to UTC.

用T替换空间将使其成为JSON证明。 JSON字符串中的Z表示转换为UTC。

Be sure to choose one consistent date format, either always your locale or store the dates in UTC and convert them back to your locale afterwards.

请务必选择一种一致的日期格式,或者始终是您的语言环境,或者以UTC格式存储日期,然后将它们转换回您的语言环境。

toLocaleString converts the date object to your locale format. A note options aren't supported by Safari and only in IE11, Chrome and Firefox are safe.

toLocaleString将日期对象转换为您的语言环境格式。 Safari不支持备注选项,只有IE11,Chrome和Firefox才是安全的。

You can circumnavigate this by doing:

您可以通过以下方式环游:

 var convert = new Date(date).toLocaleString();
 var splitted = convert.split(T)[0].split("-");
 var convert = splitted[2] + "-" + (splitted[1]) > 10 ? splitted[1] : "0" + splitted[1]) + "-" + (splitted[0]) > 10 ? splitted[0] : "0" + splitted[0]) + "T" + convert.split(T)[1];

Which will also yield: "‎‎2015-03-15‎T02‎:30‎:‎00"

这也将产生:“2015-03-15 T02:30:00”

#2


0  

Figured that the there is no simple and neat way for this than to use momentjs. I had to build out the string using the various formatting options available.. It did work for me but not sure if there a neater way.

认为没有简单而简洁的方法比使用momentjs。我不得不使用各种格式选项来构建字符串..它确实对我有用,但不确定是否有更简洁的方法。

Here is my formatter for anyone else who might need this:

这是我可能需要的其他任何人的格式化程序:

 var DateObjects = moment()   // current date. You can create for any date.
 var LocalISODateString = DateObjects.format('YYYY-MM-DDTHH:mm:ss.SSS')+'Z'

#1


0  

Use

 var date = "2015-03-14T21:00:00.000Z";
 //toLocaleString will output: 15-‎3‎-‎2015‎ 02‎:30‎:‎00
 var convert = new Date(date).toLocaleString(undefined, {second: "2-digit", minute: "2-digit", hour: "2-digit",  year: 'numeric', day: "2-digit", month: "2-digit"}).replace(/\s/, "T").replace(/(\d{2})-(\d{2})-(\d{4})/, "$3:$2:$1"){4})/, "$3")//will parse it to your local time

 //will output: "‎‎2015-03-15‎T02‎:30‎:‎00"

Replacing the space with a T will make it JSON proof. The Z in the JSON string means convert to UTC.

用T替换空间将使其成为JSON证明。 JSON字符串中的Z表示转换为UTC。

Be sure to choose one consistent date format, either always your locale or store the dates in UTC and convert them back to your locale afterwards.

请务必选择一种一致的日期格式,或者始终是您的语言环境,或者以UTC格式存储日期,然后将它们转换回您的语言环境。

toLocaleString converts the date object to your locale format. A note options aren't supported by Safari and only in IE11, Chrome and Firefox are safe.

toLocaleString将日期对象转换为您的语言环境格式。 Safari不支持备注选项,只有IE11,Chrome和Firefox才是安全的。

You can circumnavigate this by doing:

您可以通过以下方式环游:

 var convert = new Date(date).toLocaleString();
 var splitted = convert.split(T)[0].split("-");
 var convert = splitted[2] + "-" + (splitted[1]) > 10 ? splitted[1] : "0" + splitted[1]) + "-" + (splitted[0]) > 10 ? splitted[0] : "0" + splitted[0]) + "T" + convert.split(T)[1];

Which will also yield: "‎‎2015-03-15‎T02‎:30‎:‎00"

这也将产生:“2015-03-15 T02:30:00”

#2


0  

Figured that the there is no simple and neat way for this than to use momentjs. I had to build out the string using the various formatting options available.. It did work for me but not sure if there a neater way.

认为没有简单而简洁的方法比使用momentjs。我不得不使用各种格式选项来构建字符串..它确实对我有用,但不确定是否有更简洁的方法。

Here is my formatter for anyone else who might need this:

这是我可能需要的其他任何人的格式化程序:

 var DateObjects = moment()   // current date. You can create for any date.
 var LocalISODateString = DateObjects.format('YYYY-MM-DDTHH:mm:ss.SSS')+'Z'