Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date/time in GMT to EST in Javascript

Tags:

javascript

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

like image 898
Iris Avatar asked Oct 09 '09 19:10

Iris


2 Answers

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/

like image 150
Jim Avatar answered Sep 28 '22 08:09

Jim


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.

like image 33
Parivesh Jain Avatar answered Sep 28 '22 09:09

Parivesh Jain