Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set date always to eastern time regardless of user's time zone

I have a date given to me by a server in unix time: 1458619200000

NOTE: the other questions you have marked as "duplicate" don't show how to get there from UNIX TIME. I am looking for a specific example in javascript.

However, I find that depending on my timezone I'll have two different results:

d = new Date(1458619200000)
Mon Mar 21 2016 21:00:00 GMT-0700 (Pacific Daylight Time)

// Now I set my computer to Eastern Time and I get a different result.

d = new Date(1458619200000)
Tue Mar 22 2016 00:00:00 GMT-0400 (Eastern Daylight Time)

So how can I show the date: 1458619200000 ... to always be in eastern time (Mar 22) regardless of my computer's time zone?

like image 538
Shai UI Avatar asked Mar 24 '16 17:03

Shai UI


People also ask

Which timezone is EST?

Eastern Standard Time (EST) is the easternmost time zone in the United States. It is also used in Canada. It covers all or parts of 23 states in the US and three provinces or territories in Canada. It is also used in Mexico, the Caribbean, and Central America.

What is timezone offset in JavaScript?

Definition and Usage. 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

You can easily take care of the timezone offset by using the getTimezoneOffset() function in Javascript. For example,

var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)

dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)

var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)

Though, the locale string represented at the back will not change. The source of this answer is in this post. Hope this helps!

like image 137
Shekhar Chikara Avatar answered Sep 18 '22 09:09

Shekhar Chikara