Alex Rivera | Logout

Get date time for a specific time zone using JavaScript

Asked 2012-06-20T16:48:10.100
52

It seems that JavaScript's Date() function can only return local date and time. Is there anyway to get time for a specific time zone, e.g., GMT-9?

Combining @​Esailija and @D3mon-1stVFW, I figured it out: you need to have two time zone offset, one for local time and one for destination time, here is the working code:

var today = new Date();  
var localoffset = -(today.getTimezoneOffset()/60);
var destoffset = -4; 

var offset = destoffset-localoffset;
var d = new Date( new Date().getTime() + offset * 3600 * 1000)

An example is here: http://jsfiddle.net/BBzyN/3/

Edit
Report

1 Answer

-1

You can always get GMT time (so long as the client's clock is correct).

To display a date in an arbitrary time-zone, construct a string from the UTC hours, minutes, and seconds after adding the offset.

answered 2012-06-20T16:55:06.537

Your Answer