这个时间戳是什么格式,我怎样才能将它格式化为它自己的时间

发布于 2024-12-11 20:23:01 字数 596 浏览 0 评论 0原文

我在 JavaScript 中转换时间戳时遇到问题。

我有这个时间戳:

2011-10-26T12:00:00-04:00

我一直在尝试将其格式化为可读。到目前为止,它使用我系统的本地时间而不是时间戳中的 GMT 偏移量来转换它。我知道创建它的时区是东部时间。我现在是太平洋标准时间,所以时间偏移了 3 个小时。

而不是显示为:

Wednesday October 26, 2011 12:00 pm

它显示为:

Wednesday October 26, 2011 9:00 am

我尝试了几种不同的解决方案,但最新的解决方案在这里找到: http://blog.stevenlevithan.com/archives/date-time-format

我不太关心格式部分,因为我关心如何处理 GMT 偏移量。将不胜感激任何人可以提供的任何帮助和见解。

I have a problem converting a timestamp in javascript.

I have this timestamp:

2011-10-26T12:00:00-04:00

I have been trying to format it to be readable. So far, it converts this using the local time of my system instead of the GMT offset in the timestamp. I know the timezone that this was created in is EST. I'm in PST, so the times are being offset by 3 hours.

Instead of this showing as:

Wednesday October 26, 2011 12:00 pm

It shows as:

Wednesday October 26, 2011 9:00 am

I have tried a few different solutions, but the latest one is found here: http://blog.stevenlevithan.com/archives/date-time-format

I am less concerned with the formatting part as I am with figuring out how to handle the GMT offsets. Would appreciate any help and insight anyone can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

避讳 2024-12-18 20:23:01

日期对象在本地区域中创建。如果日期字符串是在不同的时区创建的,则需要调整日期对象以允许差异。

缩写 PST 和 EST 在网络上是不明确的,时区缩写没有标准,有些代表两个或多个区域。您应该仅使用 +/- UTC 或 GMT(或多或少是相同的东西)来表示您的区域。

您可以使用 Date.prototype.getTimezoneOffset 获取本地时区偏移量,它返回必须添加到本地时间才能获取 UTC 的偏移量(以分钟为单位)。计算创建时间字符串的偏移量并将其应用于创建的日期对象(只需根据需要添加或减去以分钟为单位的差异)。

如果您的时区是 -3 小时,则 getTimezoneOffset 将为在该时区中创建的日期对象返回 +180。如果字符串来自 -4hrs 区域,则其偏移量为 +240。所以你可以这样做:

var localDate = new Date('2011-10-26T12:00:00') // date from string;
var originOffset = 240;
var localOffset = localDate.getTimezoneOffset();
localDate.setMinutes( localDate.getMinutes() + originOffset - localOffset );

添加原点偏移量将其设置为 UTC,减去本地偏移量将其设置为本地时间。

如果服务器发送的时间字符串采用 UTC 格式,那就容易多了,这样您只需应用本地偏移量即可。

编辑

IE 不会解析带有偏移量的时间字符串,而 Chrome 则认为上述时间字符串是 UTC 并针对本地偏移量进行调整。所以不要让Date解析字符串,而是手动完成。

Date objects are created in the local zone. If the date string was created in a different time zone, then you need to adjust the date object to allow for the difference.

The abbreviations PST and EST are ambiguous on the web, there is no standard for time zone abbreviations and some represent two or zones. You should express your zone only in terms of +/- UTC or GMT (which are the same thing, more or less).

You can get the local time zone offset using Date.prototype.getTimezoneOffset, which returns the offset in minutes that must be added to a local time to get UTC. Calculate the offset for where the time string was created and apply it to the created date object (simply add or subtract the difference in minutes as appropriate).

If your time zone is -3hrs, getTimezoneOffset will return +180 for a date object created in that zone. If the string is from a zone -4hrs, its offset is +240. So you can do:

var localDate = new Date('2011-10-26T12:00:00') // date from string;
var originOffset = 240;
var localOffset = localDate.getTimezoneOffset();
localDate.setMinutes( localDate.getMinutes() + originOffset - localOffset );

Adding the origin offset sets it to UTC, subracting the local offset sets it to local time.

It would be much easier if the time string that was sent by the server was in UTC, that way you just apply the local offset.

Edit

IE will not parse a time string with an offset, and Chrome thinks that the above time string is UTC and adjusts for local offset. So don't let Date parse the string, do it manually.

深居我梦 2024-12-18 20:23:01

无论你在哪个时区,时间戳都会导致每个不同时区的本地时间不同,但它们都是正确的,并且任何检查日期的 UTC 时间的人都会得到相同的时间 - stamp:

new Date('2011-10-26T12:00:00-04:00').toUTCString()
returns

Wed, 26 Oct 2011 16:00:00 GMT

和 getTime() 在任何地方返回相同的毫秒通用时间戳:1319644800000

It doesn't matter what time zone you are- the time stamp will result in a different local time for every different time-zone, but they all will be correct, and anyone checking the UTC time of the date will get the same time-stamp:

new Date('2011-10-26T12:00:00-04:00').toUTCString()
returns

Wed, 26 Oct 2011 16:00:00 GMT

and getTime() anywhere returns the same milliseconds universal timestamp:1319644800000

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文