Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date.js parsing an ISO 8601 UTC date incorrectly

Using the javascript library Date.js I am finding that when i pass into the Date.parse() function an ISO 8601 formatted UTC 0 date I am getting an object that is representative of the same date but with the local time zone added.

For example,

Given the date: 2012-08-27T14:57:00Z (in ISO 8601 format), which is showing a time of 14:57 UTC, why would this be parsed as 14:57 GMT-400 as opposed to 10:57 GMT-400?

I have created a fiddle to show it in action.

Please let me know if there is in fact an error or if my understanding of the parsing result is incorrect.

like image 462
MindWire Avatar asked Aug 27 '12 16:08

MindWire


People also ask

Is ISO 8601 always UTC?

Date.prototype.toISOString() The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

How do I convert a date to ISO 8601?

The date. toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss.

Does ISO 8601 have timezone?

Time zones in ISO 8601 are represented as local time (with the location unspecified), as UTC, or as an offset from UTC.

What can I use instead of date parse?

If the DATEPARSE function is not available for the data that you're working with, or the field you are trying to convert is a number data type, you can use the DATE function instead. The DATE function converts a number, string or date expression to a date type.


2 Answers

Yep, it's a bug - even a reported one.

Might I recommend using Moment.js library instead? For example, this:

console.log(moment('2012-08-27T14:57:00Z').toString());

... will correctly recognize that UTC time is given.

like image 89
raina77ow Avatar answered Sep 21 '22 05:09

raina77ow


This seems like an error with Date.js. Using new Date('2012-08-27T14:57:00Z') returns the correct date.

like image 34
Rocket Hazmat Avatar answered Sep 20 '22 05:09

Rocket Hazmat