如何在javascript中正确地将日期和时区进行字符串化? [英] How to stringify dates with timezones properly in javascript?

查看:211
本文介绍了如何在javascript中正确地将日期和时区进行字符串化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,我在UTC中有一个日期,我想对其进行字符串化和解析,但要保持它为UTC.我做了这段代码

In javascript, I have a date in UTC, and I want to stringify it and parse it but maintain it's UTC. I did this code

var f = {f:new Date("2019年5月27日星期一20:11:13 GMT-0400(东部夏令时间)")

var f = { f : new Date("Mon May 27 2019 20:11:13 GMT-0400 (Eastern Daylight Time)")}

未定义

JSON.stringify(f)

JSON.stringify(f)

"{" f:" 2019-05-28T00:11:13.000Z}"

"{"f":"2019-05-28T00:11:13.000Z"}"

JSON.parse(JSON.stringify(f))

JSON.parse(JSON.stringify(f))

{f:"2019-05-28T00:11:13.000Z"}

{f: "2019-05-28T00:11:13.000Z"}

您可以看到,我对它进行了字符串化处理后,更改为第二天.然后,当我解析它时,它将它保留为字符串,甚至是第二天.我想要它,以便在解析它之后,我得到Mon May 27 2019 20:11:13 GMT-0400 (Eastern Daylight Time)的Date对象.

You can see that after I stringified it, it changed to the next day. And then when I parse it, it kept it as a string and even of the next day. I want it so that after I parse it, I get back the Date object of Mon May 27 2019 20:11:13 GMT-0400 (Eastern Daylight Time).

有人知道怎么了吗?

谢谢

推荐答案

您可以看到,我对它进行了字符串化处理后,更改为第二天

You can see that after I stringified it, it changed to the next day

不,不是.将格林尼治标准时间2019年5月27日20:11:13(东部夏令时间)"解析为时间值1559002273000,该时间值定义了一个时间点,该时间点是1970-01-01T00:00:00Z之后的毫秒数.等效于2019-05-28T00:11:13.000Z(以及其他时区中的其他时间戳).例如.在设置为澳大利亚东部时区的系统上,您会收到"Tue May 28 2019 10:11:13 GMT + 1000(AEST)".

No, it didn't. "Mon May 27 2019 20:11:13 GMT-0400 (Eastern Daylight Time)" was parsed to a time value of 1559002273000, which defines a moment in time that is that many milliseconds after 1970-01-01T00:00:00Z. It's equivalent to 2019-05-28T00:11:13.000Z (and other timestamps in other time zones). E.g. on a system set to an eastern Australian timezone you'd get "Tue May 28 2019 10:11:13 GMT+1000 (AEST)".

我想要它,以便在解析之后,我可以返回2019年5月27日星期一5月27日GMT-0400(东部夏令时间)的Date对象

I want it so that after I parse it, I get back the Date object of Mon May 27 2019 20:11:13 GMT-0400 (Eastern Daylight Time)

日期对象不知道原始时间戳的时区,它仅存储时间值. toString 生成的字符串仅使用主机系统时区设置来生成ECMA-262指定格式的本地"时间戳.请注意,时区名称取决于实现,并且由于它们不是标准化的,因此名称和缩写在实现之间会有所不同.例如. Safari显示"AEST",而Firefox显示澳大利亚东部标准时间".

The date object has no knowledge of the timezone of the original timestamp, it just stores the time value. The string produced by toString just uses the host system timezone setting to generate a "local" timestamp in the format specified by ECMA-262. Note that the timezone name is implementation dependent and since they aren't standardised, the names and abbreviations differ between implementations. E.g. Safari shows "AEST" and Firefox "Australian Eastern Standard Time".

您可以使用 toLocaleString 生成不同时区的时间戳,但是date对象不知道原始时区是什么,格式可能不是您想要的格式.

You can use toLocaleString to generate timestamps for different timezones, but the date object doesn't know what the original timezone was and the format may not be what you want.

此外, toLocaleString 使用 IANA时区标识符(例如非洲/金沙萨(Kinshasa),这与用于推断适用时区的地理位置有关,而不是与诸如东部夏令时间"之类的通用名称有关,这些名称没有标准化,可能会模棱两可或晦涩难懂. IANA指定者意味着可以比其他指定者更容易地应用夏时制和历史时区偏移更改之类的东西.

Also, toLocaleString uses IANA timezone identifiers (e.g. Africa/Kinshasa), which relate to geographic locations that are used to deduce the applicable timezone rather than common names like "Eastern Daylight Time" which are not standardised and can be ambiguous or obscure. The IANA designators mean things like daylight saving and historic timezone offset changes can be applied more easily than with other designators.

这篇关于如何在javascript中正确地将日期和时区进行字符串化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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