简体   繁体   中英

Retrieving the timezone offset that was already input

I create a date like this:

var date = new Date('Wed, 19 Mar 2014 18:17:00 +0200');

This resolves to:

Wed Mar 19 2014 17:17:00 GMT+0100 (Central European Standard Time)

Is there a way to retrieve the "+0200" portion from the date object once it is created? I am trying to get this without parsing the input string and without the use of external libraries.

EDIT: When I use

date.getTimezoneOffset();

It returns "-60", which corresponds to the local timezone offset, which in my case is GMT+0100. The question I am asking is whether the "+0200" from the input is lost in the Date object upon creation, or is it stored somewhere?

You can retrieve the timezoneoffset with date.getTimezoneOffset(); and use that to calculate it

From the Mozilla MSDN

The getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale.

date.getTimezoneOffset()

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight saving time prevents this value from being a constant even for a given locale.


A date object is stored as the number of milliseconds since the Unix epoch. So your input string is parsed and stored as a primitive number. So no, you can't retrieve aspects of your original input after it has been converted to a date object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM