0

I live in Arizona which does not observe Daylight Saving Time. I have a string date/time that I want to convert to different time zones.

var stringDateTime = "20181022090522";
stringDateTime = convertTimeZone(stringDateTime, "US/Arizona", "US/Eastern");
alert (stringDatetime);

Output would be "20181022120522" (Eastern Time is currently 3 hours ahead of Arizona time, time changes from 9:05AM to 12:05PM). However, after Daylight Saving goes, Eastern Time would be 2 hours ahead of Arizona. So an input of "20181201050954" would output "20181201070954" (time changes from 05:09AM to 7:09AM).

I need the "convertTimeZone" function to take the input of the string, the timezone it originates from, and the timezone to convert to. The string input and output will always be yyyyMMddHHmmss.

I also need to make sure the date part gets updated if it's close to midnight.

var stringDateTime = "20181231234502"; // (12/31/2018 11:45:02PM)
stringDateTime = convertTimeZone(stringDateTime, "US/Arizona", "US/Eastern");
alert (stringDatetime);

Output would be "20190101014502" (1/1/2019 1:45:02AM).

I need to be able to do this without involving any external javascript libraries (I can't use moment.js etc). Most of the examples I've seen have needed this or a similar library.

Pryach
  • 391
  • 2
  • 8
  • 18
  • What is your current implementation of `convertTimeZone`? –  Oct 22 '18 at 16:31
  • I don't really have anything. Anything I've tried so far hasn't worked. – Pryach Oct 22 '18 at 16:46
  • `Anything I've tried` Share with us your attempts. We are happy to help you get it working, but we are less keen about writing code for you. –  Oct 22 '18 at 16:46

1 Answers1

0

How to initialize javascript date to a particular timezone

datetime is stored in UTC and formatted by default to your local timezone.

t1  = new Date();  //my timezone
//change datetime display to these timezones
t2  = t1.LocaleString("en-US", {timeZone: "America/New_York"})
t3  = t1.toLocaleString("en-US", {timeZone: "America/Los_Angeles"})
console.log(t1);
console.log(t2);
console.log(t3);
JohnC
  • 2,687
  • 1
  • 22
  • 30