I want to calculate the offset from ‘users time’ to ‘WET/WEST’.
I get the users offset with new Date().getTimezoneOffset()
.
But how do I get the offset for WET/WEST So that I can calcluate the combined offset of both?
For example, if the user is in central europe time (CET/CEST), in winter the combined offset would be -60 (CET) + 0 (WET) = -60. In summer, it would be -120 (CEST) + 60 (WEST) = -60. In this case it is always -60 but the user could also have a timezone without DST.
Is this possible without format it to a string and read out the new timezone from that string?
Advertisement
Answer
You can’t use time zone abbreviations reliably for input, as they can be interpreted in many different ways. For example, CST
might be either “Central Standard Time” in North America, or “China Standard Time”, or “Cuba Standard Time”. While some abbreviations like WET
and WEST
are unique, many are ambiguous. Refer to the list of time zone abbreviations here.
Instead, you need to know the IANA time zone identifier. One location that uses WET/WEST is Portugal, which as the IANA identifier of "Europe/Lisbon"
. You can find a list of identifiers here. Picking the correct identifier is important, as time zones change over time. Each identifier reflects the particular history of each region.
One you know the IANA time zone identifier, then you have options for how to use it:
In some modern browsers that fully support the time zone features of the ECMAScript Internationalization API (ECMA-402), you can do the following:
var d = new Date(); var s = d.toLocaleString(undefined, { timeZone: "Europe/Lisbon" })
This will convert the provided date and time to the correct time zone during formatting. Passing
undefined
in the first parameter will use the current locale for formatting.There are a few downsides to this approach, as it is not yet implemented in all browsers, and there is no API for just retrieving the raw time zone offset of a particular point in time.
You can consider using a library that implements this functionality. I list several of them here. My personal preference is for moment.js with the moment-timezone addon, which you can do the following:
var m = moment.tz("Europe/Lisbon"); var s = m.format();
You can pass parameters to the
format
method to display the output however you like. You can also convert an existing time, such as:var m = moment.utc("2016-01-01T00:00:00").tz("Europe/Lisbon"); var s = m.format();
You can also get the offset for a particular moment in time like so:
var m = moment.utc("2016-01-01T00:00:00").tz("Europe/Lisbon"); var offsetInMinutes = m.utcOffset(); var offsetAsString = m.format("Z");
You can write your own code for handling a particular time zone. Though this can be error prone and I don’t generally recommend it. Updates can be particularly difficult if you go down this route.
Do also keep in mind that the offset for a particular time zone will vary depending on the date and time in effect. Therefore, new Date()
which represents “now” may or may not always be the correct input, depending on your scenario.