Javascript转换时区问题 [英] Javascript convert timezone issue

查看:334
本文介绍了Javascript转换时区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对在当前时区转换日期时间的问题。

I am facing an issue in converting datetime in current timzone.

我从服务器收到这个日期字符串,格式为2015-10-09T08:00:00,这是中央时间,但是当我使用新的日期(strDate)在GMT + 5它返回我下面是不正确的。

I am receiving this date string from server in a format "2015-10-09T08:00:00" which is Central Time but when I convert this date time using new Date(strDate) in GMT+5 its returning me below which is incorrect.

var dateObj = '2015-10-09T08:00:00';
new Date(dateObj); // return me below
Fri Oct 09 2015 13:00:00 GMT+0500 (PKT)

另一种使用方法是通过添加时区偏移量并返回正确的结果进行转换,但在启用夏令时时会出现异常失败。

Another way I used is to convert by adding timezone offset and its returning me right result but defiantly failed when daylight saving activated.

dateObj2 = '2015-10-09T08:00:00'+'-06:00';
new Date(dateObj2)// return me below
Fri Oct 09 2015 19:00:00 GMT+0500 (PKT)

如果有人帮忙或建议我使用JavaScript中的夏令时来处理时区转换,我感到欣慰

I appreciated if anyone help OR suggest me efficient way to handle to timezone conversion with daylight saving in JavaScript?

谢谢。

推荐答案

请注意,您编写的代码行为在浏览器之间有所不同:

Note that behavior of the code you wrote differs between the browsers:

new Date('2015-10-09T08:00:00').toString()

// "Fri Oct 09 2015 10:00:00 GMT+0200 (Romance Daylight Time)" // Chrome 46 on Windows 8.1
// "Fri Oct 09 2015 08:00:00 GMT+0200 (Romance Daylight Time)" // Firefox 41 on Windows 8.1
// "Fri Oct 09 2015 08:00:00 GMT+0200 (Romance Daylight Time)" // IE11 on Windows 8.1
// "Fri Oct 9 08:00:00 UTC+0200 2015" // IE10 emulation
// "Fri Oct 9 10:00:00 UTC+0200 2015" // IE9 emulation
// on IE8 it even returns NaN!

(我的时区是巴黎)

因此,Firefox和IE会将提供的日期解释为与用户本地时区相同的指定日期,而Chrome将其解释为UTC,打印时将被转换为用户的时区。

So, Firefox and IE interpret the provided date as specified as if it were in local timezone of the user, whereas Chrome interprets it as UTC, and when printed, it gets converted to user's timezone.

检查MDN文档,这是因为EcmaScript 5和EcmaScript 6(2015)规范的差异。看来,Chrome遵循ES5规范,Firefox和IE11遵循ES6规范。

Checking the MDN docs, this is due to differences in EcmaScript 5 and EcmaScript 6 (2015) specifications. It seems that Chrome follows ES5 spec while Firefox and IE11 follow ES6 spec.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse #ECMAScript_5_ISO-8601_format_support (强调我的)


日期时间字符串可能是ISO 8601格式。例如,
2011-10-10(只是日期)或2011-10-10T14:48:00(日期和时间)可以
被传递和解析。 UTC时区用于解释不包含时区信息的ISO 8601格式的参数
(注意:
ECMAScript 2015指定不带时间
区域的日期时间字符串被视为本地的,而不是UTC)。

不幸的是日期对象因其奇怪和跨浏览器的不一致而出名,特别是对于不明确的输入。

Unfortunately Date object in JavaScript is famous for its quirks and cross-browser inconsistencies, particularly on ambiguous input.

我写了 here
你如何利用 moment.js 或本机 Intl API,以确保您的日期不会转换为用户的时区(秘密是使用UTC操作方法)。

I wrote here how you can leverage moment.js or native Intl API to make sure your date will not be converted to user's timezone (the secret is to use UTC manipulating methods).

一般来说,最好是始终指定时间和UTC偏移量,或者仅指定UTC时间戳,以确保您的输入是明确的。

In general it's best to always specify either both time and UTC offset, or just a UTC timestamp, to make sure your input is unambiguous.

回到你的例子,你可以使用以下代码:

Coming back to your example, you can use following code:

moment('2015-10-09T08:00:00-06:00')
 .utcOffset(+300).locale('en_gb').format("LLLL")
// "Friday, 9 October 2015 19:00" cross-browser

其中您说这是UTC-0600中的日期,请转换并打印为UTC + 0500(+300分钟)。然后,您可以传递您想要打印的区域设置(即语言+文化特定设置,例如 en_gb 使用24小时制,而 en_us 12小时钟),并使用moment.js支持的日期格式。

in which you say "this is date in UTC-0600, please convert and print it as UTC+0500 (+300 minutes)". Then you can pass in which locale you want it printed (i.e. language + culture specific settings, e.g. en_gb uses 24 hour clock while en_us 12-hour clock) and use multitude of date formats supported by moment.js.

这篇关于Javascript转换时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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