-2

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

I referred the above link for apply timezones.

THe below code is working fine for EST

 var dt = new Date();
console.log(dt);
alert(dt.getTimezoneOffset());

dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); 

var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate);

Here the offset is var offset = -300; for EST. Similarly I have the list of timezones like

CST, MST, PST, AKST, HST

Here how to calculate the offset for each . Or any other better way to calculate date time new date() with this timezones.

Sam Hanson
  • 1,317
  • 4
  • 22
  • 48

1 Answers1

-1

The offset may be different in summer/winter depending if there is a daylight saving time for the timezone. It could be cumbersome implementing it yourself and prone to bugs. I would use a library for that, for example momentjs with moment-timezone plugin https://momentjs.com/timezone/

Dainius Lukša
  • 966
  • 1
  • 11
  • 22