6

In Javascript, how can I convert date/time in GMT to EST irrespective of user settings?

Nate
  • 30,286
  • 23
  • 113
  • 184
Iris
  • 1,321
  • 4
  • 17
  • 19

3 Answers3

5
var tmpDate = New Date("enter any valid Date format here")

The javascript Date() function will automatically convert it to your local time.

Example:

var tmpDate = new Date("Fri Jul 21 02:00:00 GMT 2012");
alert(tmpDate);
//Result: Fri Jul 20 22:00:00 EDT 2012

Try some different values at jsfiddle: http://jsfiddle.net/R3huD/

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Jim
  • 51
  • 1
  • 3
  • Hello Jim, I changed you w3schools link to a jsfiddle link. W3Schools is very much hated for various reasons in the StackOverflow community, see [here](http://meta.stackoverflow.com/search?tab=relevance&q=w3schools). Also, thats probably the easiest/best approach to the problem (I use the same in a work related project). – Kijewski Jul 21 '12 at 02:32
1

i was surprise to find the simplest solution.

If you have date in GMT, and when you create date in browser it always create in that time zone.

Simplest way is create date object with GMT itself and then do below

starTime.setHours(starTime.getHours()+(starTime.getTimezoneOffset()/60));

That's it. Even if you have date of future after day light saving like after November then also it will also work.

Parivesh Jain
  • 71
  • 2
  • 10
0

See here: https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html

all you have to do is get the time in miliseconds and then add the offset in milliseconds and then shift back to a date time object

Community
  • 1
  • 1
Daniel
  • 2,331
  • 3
  • 26
  • 35
  • 1
    look carefully at the code. Though the output string exactly matches the date in the target timezone, the final date object will still be in local timezone. – Praveen Lobo Nov 18 '11 at 22:58