14

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.

Nocklas
  • 1,367
  • 4
  • 15
  • 27
  • Is it? I agree that if I knew how to specifiy that the Date object is in a specific time zone, this should be enough to be able to convert between time zones. But I'm not aware of a way to do this. – Nocklas Nov 22 '12 at 23:37
  • https://js-joda.github.io/js-joda/ Technically this question is out-of-scope (software recommendations) hence the comment. Joda is hard to use, but I remember it did something correctly that Moment failed at. – Chloe Jul 24 '18 at 16:46

4 Answers4

11

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.

unknown
  • 846
  • 3
  • 15
  • 38
9

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 ;)

Stephen
  • 5,362
  • 1
  • 22
  • 33
4

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.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • `moment-timezone.js` requires `moment.js` and timezone data. The docs for Moment Timezone list which `.js` files you'll need. – 700 Software Nov 03 '16 at 15:20
-3

You can have a look at this post: How to get user timezone using jquery?

It seems that you don't need any library to convert timezones.

-- EDIT (for people who are too leasy to read other question's answers on Stackoverflow)

You can use the standard Date object this way:

var offset = (new Date).getTimezoneOffset()/60

And for those who are too leasy to read specifications...

This will give you the hour according to universal time: var hour = (new Date).getUTCHours() And this will give you the hour according to the local time: var hour = (new Date).getHours()

Understood now?

Community
  • 1
  • 1
  • I don't see how that post solves my problem. If you do, then please enlighten me. – Nocklas Nov 22 '12 at 23:22
  • It seems that you can manipulate a date, even with timezone support just with the a "Date" object. Just have a look at this: http://www.w3schools.com/jsref/jsref_obj_date.asp – Charles-Édouard Coste Nov 23 '12 at 10:08
  • Timezones (e.g. America/Los_Angeles) are used in calendaring applications because events are scheduled in local time. Whatever the timezone offset is on that day (changing daylight savings rules etc.), the local time at which the event takes place must remain the same. For these applications we often need to convert timezone+localtime into universal time and vice-versa. – Martin Jambon Mar 19 '14 at 18:43
  • Yeah, you're right! But just a question... What is the purpose of this comment? – Charles-Édouard Coste Mar 19 '14 at 18:53
  • I guess that nobody on this page clicked on the link I posted. If you go and read answers on [How to get user timezone using jquery?](http://stackoverflow.com/questions/8090549/how-to-get-user-timezone-using-jquery), you will see that the answer was actually : `var offset = (new Date()).getTimezoneOffset()` and so... **You don't need any library to get it**. – Charles-Édouard Coste Mar 19 '14 at 19:00
  • I did and left a comment there. Guessing the user's timezone is best achieved with the jstz library (jsTimezoneDetect). This is not the question here though. – Martin Jambon Mar 19 '14 at 19:33
  • jsTimezoneDetect is just a wrapper for the Date object (as you can see if you have a look at [the code](https://bitbucket.org/pellepim/jstimezonedetect/src/f9e3e30e1e1f53dd27cd0f73eb51a7e7caf7b378/jstz.js?at=default)) – Charles-Édouard Coste Mar 20 '14 at 08:25
  • Saying that these libraries are just "wrappers" of the Date object is seriously underestimating the complexity that they manage for you. After you write the logic to deal with the offset data you get back from the Date object, guess what, you have a library that looks a lot like one of the timezone conversion libraries. – Timothy Lee Russell Mar 17 '16 at 00:05
  • @Charles-EdouardCoste That's not how to get a time zone. That's how to get the browser's current UTC offset. They are different things. – Suncat2000 Nov 09 '16 at 22:30