codeandcloud
codeandcloud

Reputation: 55200

date string that will ignore timezone offset

I am at (UTS-05:00) Eastern Time (US & Canada)

i.e, new Date().getTimezoneOffset() == 300 seconds.

Now, I have a API endpoint (JSON) that returns a date string like this.

{
    someDate: '2016-01-01T00:40:00.000+00:00'
}

Here, I pass it to Date constructor like this

var dateString = "2016-01-01T00:40:00.000+00:00";
var someDay = new Date(dateString);
console.log(someDay)

Mozilla Firefox console shows

Date {Fri Jan 01 2016 00:40:00 GMT-0500 (Eastern Summer Time)}

Google Chrome console shows

Thu Dec 31 2015 19:40:00 GMT-0500 (Eastern Standard Time)

Chrome is taking the TimezoneOffset into consideration and Firefox is not. What can I do to get a Date that doesn't take Offset into consideration like FireFox in Chrome?

Upvotes: 4

Views: 659

Answers (2)

codeandcloud
codeandcloud

Reputation: 55200

This hack works (not very clean, but does the job)

var dateString = '2016-07-27T01:40:30';
var dateParts = dateString.split(/-|T|:/);
var saneDate = new Date(
    +dateParts[0], 
    dateParts[1] - 1, 
    +dateParts[2], 
    +dateParts[3], 
    +dateParts[4], 
    +dateParts[5]);
console.log(saneDate);

Upvotes: 0

Yan Pak
Yan Pak

Reputation: 1867

You can do it by:

 var dates = '2016-01-01T00:40:00.000+00:00'.split(/-|T|:/);
 var newDate = new Date(dates[0], dates[1]-1, dates[2], dates[3], dates[4]);

Upvotes: 3

Related Questions