我有一个UTC字符串,我想在JavaScript中将其转换为UTC日期对象 [英] I have a UTC string and I want to convert it to UTC Date Object in JavaScript

查看:80
本文介绍了我有一个UTC字符串,我想在JavaScript中将其转换为UTC日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript,我有一个JSON调用,它将我的UTC DateTimes作为字符串返回,我想将它们转换为UTC Date对象。有没有人这样做过?我根本不想去当地时间。

解决方案

在任何模糊的现代浏览器上,你只需将它传递给日期最后带Z

  var dt = new Date(yourString +Z); 

该格式是在2009年12月的ES5规范中为JavaScript定义的。 Z对于确保字符串被解释为UTC而非本地时间¹非常重要。



日期对象跟踪特定时刻,这与时区无关。如果您使用日期 UTC 方法( getUTCFullYear getUTCMonths 等),您将以UTC格式访问该瞬间。 (如果您使用非UTC方法,则会将其翻译为当地时间。)



示例:



< pre class =snippet-code-js lang-js prettyprint-override> var str =2016-11-22T17:14:00; var dt = new Date(str +Z) ; console.log(UTC string:); console.log(dt.toUTCString()); console.log(Local string); console.log(dt.toString()); console.log(Hours UTC:+ dt.getUTCHours()); console.log(当地时间:+ dt.getHours());






¹这方面有一些历史。当格式最初添加到ES5规范时,假设是ISO-8601的子集,但ES5规范表示没有时区指示符表示UTC,而在ISO-8601中,没有时区指标表示当地时间。这导致了不一致的实现,其中一些符合ES5规范,而其他一些符合ISO-8601。该漏洞在2015年6月的ES2015规范中得到修复。现在,JavaScript的日期/时间格式确实是ISO-8601的一个子集,但JavaScript的 Date 总是有一个时间组件(一直到毫秒),而ISO-8601的概念是价值可能只是精确的特定值。也就是说,JavaScript中的 2016-11-22 是特定日期时间,但在ISO-8601中,它只是日期(没有时间)暗示)。因此,当前(ES2016)JavaScript规范


当时区偏移不存在时,仅日期表格被解释为UTC时间和日期时间表格被解释为当地时间。


所以 2016-11-22 在UTC午夜被解释为2016-11-22,但 2016-11-22T00:00:00 在当地时间午夜被解释为2016-11-22。奇怪但真实。当然,规范中的这种最新语言可能还没有被所有实现正确实现(例如,我注意到Chrome 54错了)。



底线:您需要Z以确保将字符串解析为UTC。


I am using JavaScript, I have a JSON call that returns my UTC DateTimes as strings and I want to convert them to UTC Date objects. Has anyone done this? I do not want to go to local time at all.

解决方案

On any vaguely-modern browser, you just pass that into Date with a "Z" on the end:

var dt = new Date(yourString + "Z");

That format was defined for JavaScript in the ES5 specification in December 2009. The "Z" is important to ensure that the string is interpreted as UTC, not local time¹.

Date objects keep track of a specific instant in time, which is irrespective of timezone. If you use the Date's UTC methods (getUTCFullYear, getUTCMonths, etc.), you'll access that instant in UTC. (If you use the non-UTC methods, you'll access it translated to local time.)

Example:

var str = "2016-11-22T17:14:00";
var dt = new Date(str + "Z");
console.log("UTC string:");
console.log(dt.toUTCString());
console.log("Local string");
console.log(dt.toString());
console.log("Hours UTC:   " + dt.getUTCHours());
console.log("Hours local: " + dt.getHours());


¹ There's a bit of history in regard to that. When the format was originally added to the ES5 specification, it was supposed to be a subset of ISO-8601, but the ES5 spec said that no timezone indicator meant UTC, whereas in ISO-8601, no timezone indicator means local time. That lead to inconsistent implementations, where some were true to the ES5 specification, and others were true to ISO-8601. The bug was fixed in the ES2015 specification in June 2015. Now JavaScript's date/time format really is a subset of ISO-8601, except that JavaScript's Date always has a time component (all the way down to milliseconds), while ISO-8601 has the concept that a value may only be as specific as it is precise. That is, 2016-11-22 in JavaScript is a specific date and time, but in ISO-8601, it's just the date (no time is implied). Consequently, the current (ES2016) JavaScript specification says:

When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

So 2016-11-22 is interpreted as 2016-11-22 at midnight UTC, but 2016-11-22T00:00:00 is interpreted as 2016-11-22 at midnight local time. Bizarre, but true. Of course, this latest language in the specification may not be correctly implemented by all implementations yet (I note that Chrome 54 gets it wrong, for instance).

Bottom line: You need that "Z" to ensure the string is parsed as UTC.

这篇关于我有一个UTC字符串,我想在JavaScript中将其转换为UTC日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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