Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get date time for a specific time zone using JavaScript

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/

like image 841
Yang Avatar asked Jun 20 '12 16:06

Yang


People also ask

How do you find time according to time zones?

The getTimezoneOffset() method returns the time difference between Greenwich Mean Time (GMT) and local time, in minutes. For example, If your time zone is GMT+2, -120 will be returned. Note: This method is always used in conjunction with a Date object.

How do I get a specific timezone offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

What timezone is JavaScript date?

JavaScript's internal representation uses the “universal” UTC time but by the time the date/time is displayed, it has probably been localized per the timezone settings on the user's computer. And, indeed, that's the way JavaScript is set up to work.

How do I get current time zone in JavaScript?

JavaScript Date getTimezoneOffset() getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.


1 Answers

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>
like image 147
Esailija Avatar answered Oct 13 '22 14:10

Esailija