Question

I need to get a timezone from a time, date is not important, but daylight savings is.

something like:

timezone = function("15:00");

Is there a simple way to do this?

Was it helpful?

Solution

I dont think you can get the timezone from the time but you might get some help from Date.prototype.getTimezoneOffset()

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

Example:

var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;

OTHER TIPS

No, of course not. Think about it, you're passing 15:00 to that function, presumable denoting it's 3PM. But in what timezone is it 3 PM? No way of knowing. It's like me saying it's quarter to, without saying what hour it's quarter to to.

The only way you can get a timezone in JS is by using the Date object, but that just shows the timezone of the machine on which your code is running, nothing about the TZ that "created" the time you're processing.

Also note that daylight saving isn't a global phenomenon, quite the contrary. AFAIKT, there isn't a single time-zone where DST has always been in place...

In order to get TimeZone information you need more than a Date (and an offset). You need a location.

Javascript does not know the location that it resides in but it does know the current offset from UTC. That is different than a Time Zone. The daylight savings time issue play havoc with this key difference.

This has posed problems when dealing with server applications that know their timezone and report dates as being in a specific Time Zone.

My rule of thumb has been fairly simple in this regard.

Always use Long or long (a 64 bit number) to store, pass and process dates times or intervals, only convert to Date, Calendar or DateTime objects when interacting with people.

Once you have a date object, such as with new Date(), you can use .getTimezoneOffset() to get the number of minutes between the date's object and UTC, which is timezone information you can use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top