D3 DateTime解析器考虑了时区 [英] D3 DateTime parser takes into account timezone

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

问题描述

我有这个URL,我需要解析并将 mydate 时间戳转换为UTC日期时间(在Javascript中)。

I have this URL and I need to parse and convert the mydate timestamp into a UTC datetime (in Javascript).

http://www.blablabla.com/#mydate=2009-10-12T22:09:28.000

显然,URL中编码的日期只是字符串,因此它不会传达任何时区信息:但它代表的是UTC时间。

Obviously the date encoded in the URL is just string so it doesn't convey any timezone information: however it is meant to represent a UTC time.

将UTC时间戳字符串转换为UTC日期时间似乎非常简单但是我遇到了这个问题:

Converting a UTC timestamp string into a UTC Datetime seems to be quite straightforward however I get this problem:

我正在使用D3时间解析器要做到这一点,这里重要的一点:

I'm using D3 time parser to do it, here the important bit:

var iso = d3.time.format.utc("%Y-%m-%dT%H:%M:%S.%L");
var dateObj = iso.parse(mydate);

mydate 是一个字符串,它只是包含时间戳位的剪辑版本的URL:

The mydate is a string and it's just a clipped version of the URL containing the timestamp bit:

2009-10-12T22:09:28.000

.parse 函数的输出为:

 Mon Oct 12 2009 23:09:28 GMT+0100 (GMT Daylight Time)

我原本预计会这样(这就是我想要的):

I'd have expected this (this is what I want):

Mon Oct 12 2009 22:09:28

操作系统和浏览器设置为UTC时间(这里我们目前在GMT)。
为什么我得到(GMT日光时间)部分而不是普通的UTC字符串?

The OS and the browser are set to UTC time (here we are currently in GMT). So why I get the (GMT Daylight Time) part instead of a plain UTC string?

编辑

即使只有一个简单的新日期(2009,9,12,22,29,28)我也会遇到同样的问题(没有D3) ,没有MomentJS,只是一个简单的JavaScript DateTime对象)

Even with a simple new Date(2009, 9, 12, 22, 09, 28) I get the same issue (no D3, no MomentJS, just a plain JavaScript DateTime object)

推荐答案

值GMT + 0100(GMT日光时间)是Windows的说英国夏令时(或BST)的有趣方式。 2009年,BST从3月29日开始运行到10月25日,因此您的10月12日日期在该范围内。

The value "GMT+0100 (GMT Daylight Time)" is Windows's funny way of saying "British Summer Time" (or, BST). In 2009, BST ran from March 29 to October 25, thus your date of October 12 is in that range.

您的时区未设置为(UTC)协调世界时,设置为(UTC)Dublin,Edinburgh,Lisbon,London。括号中的值仅表示标准偏移量。它不反映夏令时。

Your time zone is not set to "(UTC) Coordinated Universal Time", it's set for "(UTC) Dublin, Edinburgh, Lisbon, London". The value in parenthesis represents the standard offset only. It doesn't reflect daylight saving time.

最后,输出实际上是 Date 对象,而不是字符串。 JavaScript 日期对象根据运行它的计算机的时区以本地时间表示其大部分输出。如果你想要UTC时间,那么你可以使用 .toISOString()而不是 .toString(),尽管如此将采用 ISO 8601 扩展格式,而不是人类可读的格式。

Lastly, the output is actually a Date object, not a string. The JavaScript Date object presents most of its output in terms of local time, based on the time zone of the computer where it is running. If you want the UTC time, then you can use .toISOString() instead of .toString(), though that will be in ISO 8601 extended format, rather than a human-readable format.

如果您想要一个人类可读的基于UTC的格式,因为您已经使用了D3,那么请执行以下操作:

If you want a human-readable UTC-based format, since you're using D3 already, then do something like the following:

var iso = d3.time.format.utc("%Y-%m-%dT%H:%M:%S.%L");
var dateObj = iso.parse(mydate);

var fmt = d3.time.format.utc("%c"); // or whatever format you wish
var str = fmt.format(dateObj);

参考 D3的时间格式文档了解更多详情。

这篇关于D3 DateTime解析器考虑了时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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