Iris
Iris

Reputation: 1341

Convert date/time in GMT to EST in Javascript

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

Upvotes: 6

Views: 22295

Answers (3)

Parivesh Jain
Parivesh Jain

Reputation: 71

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.

Upvotes: 1

Jim
Jim

Reputation: 51

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/

Upvotes: 5

Daniel
Daniel

Reputation: 2381

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

Upvotes: 0

Related Questions