Wednesday, September 9, 2015

Convert date to another timezone in JavaScript


I am looking for a function to convert date in one timezone to another.
It need two parameters,
  • date (in format "2012/04/10 10:10:30 +0000")
  • timezone string ("Asia/Jakarta")
The timezone string is described in http://en.wikipedia.org/wiki/Zone.tab
Is there an easy way to do this?
shareimprove this question
   
3 
No, my problem is slightly different. Maybe there is no easy way. I will convert those wiki to array, and turn it to function. –  Rizky Ramadhan Apr 10 '12 at 11:38
   
Or maybe better if someone did this. So I don't have to reinvent the wheel... –  Rizky Ramadhan Apr 10 '12 at 11:39
1 
Find the UTC offset of a given city. stackoverflow.com/questions/3002910/… –  Brandon Boone Apr 10 '12 at 12:05 
1 
I want to to calculate not just the UTC offset but also Daylight saving/summer time. So the time will return correctly. –  Rizky Ramadhan Apr 10 '12 at 12:11

7 Answers

up vote49down voteaccepted
For moment.js users, you can now use moment-timezone. Using it, your function would look something like this:
function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}
shareimprove this answer
18 
please provide solution with this library instead of link to library –  msangel Feb 26 '14 at 2:05
1 
Changed the answer to this, looks like more accurate than mine. –  Rizky Ramadhan May 9 '14 at 0:56 
   
what is the format of time –  Rafee Feb 13 at 17:40
   
I wasn't sure how to add new timezones, the docs didn't help me. I'm just a programmer, not a time expert!–  renocor Jun 8 at 16:11