45

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/

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Yang
  • 6,682
  • 20
  • 64
  • 96
  • You don't need to do this at all. Please read my comments about using `UTC` methods. – Esailija Jun 20 '12 at 17:25
  • 1
    possible duplicate of [Convert date to another timezone in javascript](http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – Jonathan Feb 03 '14 at 22:50

5 Answers5

32
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>
Gatsbimantico
  • 1,822
  • 15
  • 32
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • 2
    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 Jun 20 '12 at 17:07
  • @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) – Esailija Jun 20 '12 at 17:07
  • @Yang paste the exact snippet you're using please. It should show `09:12` anyway (or `08:12` for `-9`) – Esailija Jun 20 '12 at 17:12
  • I guess what happens is newDate().getTime() already got my local time which is PST. – Yang Jun 20 '12 at 17:14
  • ok, I figured out-- you need to have two offset, one for local time and one for destination time. I'll paste the solution. – Yang Jun 20 '12 at 17:17
  • 2
    @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. – Esailija Jun 20 '12 at 17:20
  • is there a way I can get only time, say "08:55" – Deepanshu Goyal Dec 13 '13 at 06:24
  • 1
    Does this take into account daylight savings in both local and destination offsets? – Patrick Roberts Jan 15 '16 at 20:02
  • 1
    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. – Marenthyu Apr 03 '21 at 08:22
30

You can do this in one line:

let d = new Date(new Date().toLocaleString("en-US", {timeZone: "timezone id"})); // timezone ex: Asia/Jerusalem
E. Karim
  • 649
  • 7
  • 14
  • 1
    Thank you, now i can use the getDate() function's – MomasVII Jan 17 '22 at 06:22
  • 1
    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.) – Otto G Jan 06 '23 at 15:41
6
var today = new Date();  
var offset = -(today.getTimezoneOffset()/60);  
Sully
  • 14,672
  • 5
  • 54
  • 79
-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.

kennebec
  • 102,654
  • 32
  • 106
  • 127
-1

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

hdogan
  • 903
  • 6
  • 6