问题
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
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.
Converting a UTC timestamp string into a UTC Datetime seems to be quite straightforward however I get this problem:
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);
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
The output of the .parse function is:
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
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?
EDIT
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)
回答1:
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.
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.
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.
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);
Refer to D3's time formatting documentation for more details.
来源:https://stackoverflow.com/questions/33755972/d3-datetime-parser-takes-into-account-timezone