50

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/

2
  • 1
    You don't need to do this at all. Please read my comments about using UTC methods. Commented Jun 20, 2012 at 17:25
  • 1
    possible duplicate of Convert date to another timezone in javascript Commented Feb 3, 2014 at 22:50

5 Answers 5

42

You can do this in one line:

let d = new Date(new Date().toLocaleString("en-US", {timeZone: "timezone id"})); // timezone ex: Asia/Jerusalem
Sign up to request clarification or add additional context in comments.

2 Comments

This is a much better answer than the accepted one, at least these days when all modern browsers support it, as specifying a time zone by name will presumably take daylight savings time into account. (Since different time zones switch on different dates, or do not use DST at all, a simple offset is not enough.)
It's better to use undefined for the locale instead of hard-coding to en-US.
31
var offset = -8;
new Date( new Date().getTime() + offset * 3600 * 1000).toUTCString().replace( / GMT$/, "" )

"Wed, 20 Jun 2012 08:55:20"

<script>
  var offset = -8;

  document.write(
    new Date(
      new Date().getTime() + offset * 3600 * 1000
    ).toUTCString().replace( / GMT$/, "" )
  );
</script>

9 Comments

var offset = -8; var d = new Date( new Date() + offset * 3600 * 1000) alert(d); would give me incorrect time of PST (shows 03:06 instead of 10:06 now), anything wrong here?
@Yang yea, use the .getTime() patch in my newest edit. I didn't use a variable in my tests and the "+" turned it into a string concatenation. Oh and take the time in UTC which is emulating the different timezone. So the correct one would be .toUTCString not .toString (which is what alert(d) does)
@Yang you can use this solution fine as long as you use the UTC methods getUTCHours(), getUTCMinutes(), toUTCString() etc...the problem with alert(d) is that it does alert(d.toString(), where as you want toUTCString in this case.
Does this take into account daylight savings in both local and destination offsets?
As Patrick mentioned, this gets iffy with leap seconds and other timezone schenanigans like DLST. I highly recommend E. karim's solution over this one.
|
6
var today = new Date();  
var offset = -(today.getTimezoneOffset()/60);  

Comments

-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.

Comments

-1

There is simple library for working on timezones easily called TimezoneJS can be found at https://github.com/mde/timezone-js.

1 Comment

It's deprecated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.