Javascript date - saving timezone offset

I have an ISO8601 date that contains a timezone offset (see below). When I create a Date object from this, the date object is converted to my time zone (currently GMT), and the timezone offset is 0. Is there a way to get the Date () constructor to save the timezone offset?

var date = new Date("2012-01-17T12:55:00.000+01:00"); console.log(date.toString()); 

The output I get is:

 "Tue Jan 17 2012 11:55:00 GMT+0000 (GMT)" 

The output I want is:

 "Tue Jan 17 2012 12:55:00" 
+6
source share
1 answer

Not with the built-in Date object , since they only know Local (as defined by the userโ€™s browser and / or OS settings) and UTC . You can see this from the many cloned methods that the class has (for example, getHours / getUTCHours ).

getTimezoneOffset is the only time zone information that you really have, but it is also local, and it will probably give you +0 again (or +6 in my case):

 var date = new Date("2012-01-17T12:55:00.000+01:00"); console.log(date.getTimezoneOffset() / 60.0); 

You can try timezone-js (or one of its forks ), but you need to know the name of Olson's time zone , and not just the GMT / UTC offset:

 var date = new new timezoneJS.Date('2012-01-17T12:55:00.000+01:00', 'Europe/Brussels'); alert(date.getTimezoneOffset() / 60.0); // +1 
+8
source

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


All Articles