Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript library that handles time zone conversion

Is there a javascript library that can handle time zone conversions (and takes DST rules and such stuff into account)? I know there are similar questions, but none of the ones I have seen seem to have an answer that really fits my question.

I would like to create a date in time zone A and be able to manipulate it (add days, hours and stuff like that) and then convert it to another time zone B. There must be a lot of people that need this kind of functionality, so I guess there should be some library out there that I haven't found.

like image 905
Nocklas Avatar asked Nov 22 '12 23:11

Nocklas


People also ask

How does JavaScript handle different time zones?

You can't change the Date object's behavior to get it to use a different time zone just by adding or subtracting an offset. It will still use the local time zone of where it runs, for any function that requires a local time, such as . toString() and others.

What time zone does JavaScript use?

JavaScript's internal representation uses the “universal” UTC time but by the time the date/time is displayed, it has probably been localized per the timezone settings on the user's computer. And, indeed, that's the way JavaScript is set up to work.


3 Answers

MOMENT - A 5kb javascript date library for parsing, validating, manipulating, and formatting dates.

moment().zone(); this is the function you will be wanting.

You can also try out datejs, however i prefer moment library, they have good docs, and maintains delicious code.

like image 75
unknown Avatar answered Oct 12 '22 22:10

unknown


I think you want this:

https://github.com/mde/timezone-js

The parser does need a bit of a tweak to get to run without the other library the person put together (at least last I used it.. five months ago?), but otherwise this seems to be a solid library. I've used it on two very timezone sensitive, high traffic, commercial sites with great success.

It doesn't play well with jquery's date picker and momentjs (and probably other libraries) sometimes though, so be prepared to either tweak those libraries to use timezoneJS' date, or have a to/from conversion function so that you can turn a timezonejs date into a 'normal' date and vice-versa for input/output.

Otherwise, as a sample case, this will take an Asia/Singapore timezone date and turn it into an America/Los_Angeles date while respecting the daylight savings time for that year. It uses Olson timezone files, which are freely available, and parses them out into something that is actually really accurate.

Hopefully this gets you on the right path.

--Edit--

Forgot - a couple tips on using it. If you've got a limited set of timezones, I'd strongly recommend plucking those out by telling the parser to only use those - then you don't have to load a big file of all of them. Also, I got kicked once because setHours, setMinutes, etc don't follow the JS "spec" of setHours(0, 0, 0, 0) (which is hours, minutes, seconds, millis). There is an enhancement request here: https://github.com/mde/timezone-js/issues/48 I don't think that it will be at all difficult for the code modification to support that, so I hope we'll see it merged in sooner than later.. or I may just do it myself ;)

like image 25
Stephen Avatar answered Oct 13 '22 00:10

Stephen


Moment has now a dedicated librabry: moment-timezone. You can create your own tzdata using online timezone data builder.

Just like moment, its timezone counterpart is light and its API well designed.

like image 30
Brian Clozel Avatar answered Oct 12 '22 23:10

Brian Clozel