Date string that will ignore timezone offset

I am in (UTS-05: 00) Eastern Time (USA and Canada)

ie, new Date().getTimezoneOffset() == 300seconds.

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

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

Here I pass it to a Date constructor like this

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

Mozilla Firefox Console Shows

Date {Fri 01/01/2016 00:40:00 GMT-0500 (Eastern Daylight Saving)}

Google Chrome console shows

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

Chrome takes into account TimezoneOffset, but Firefox does not. What can I do to get a date that doesn't take bias into account like FireFox in Chrome?

+4
source share
2 answers

You can do it:

 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]);
+3

( , )

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);
0

Source: https://habr.com/ru/post/1649159/


All Articles