3

I am trying to get the difference between a initial date and the date as of right now. I understand when I get the date like this new Date("Feb 17, 2009 12:20:47"); the timezone it gives me is the timezone on my local computer.

I am wondering how I can get the dates in the Sydney Timezone.

var sydneyTime = new Date().toLocaleString("en-US", {timeZone: "Australia/Sydney"});
initDate = new Date("Jan 27, 2009 12:50:37");
rightNow = new Date(sydneyTime);

Question: How do I convert initDate into Sydney time?

Kiwa
  • 215
  • 2
  • 10
  • Possible duplicate of [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – flppv Mar 17 '19 at 00:54
  • The question doesn't make sense since "Jan 27, 2009 12:50:37" doesn't have a timezone, converting it to any timezone is as simple as adding an appropriate timezone name or abbreviation. – RobG Mar 17 '19 at 08:43

1 Answers1

3

This will work same as your first string. It doesn't have to be only new Date() can be a defined date as well.

let s = 'Jan 27, 2009 12:50:37';
// Parse as local
let d = new Date(s);
let sydneyTime = d.toLocaleString(undefined, {timeZone: "Australia/Sydney"});

console.log(`Original date in my location: ${s}\n\
Equivalent Sydney time      : ${sydneyTime}`);
RobG
  • 142,382
  • 31
  • 172
  • 209
flppv
  • 4,111
  • 5
  • 35
  • 54
  • That will parse the string as a local time in whatever timezone the host system is set to, then generate an equivalent time in whatever timezone Sydney has for that date and time. It will produce different results on every host with a different timezone offset. – RobG Mar 17 '19 at 08:45
  • 1
    @RobG but Author pointed in his question that he want indeed to compare his local date with Sydney equivalent. – flppv Mar 17 '19 at 11:10
  • I'm glad you were able to decipher the meaning as I wasn't. The usual caveat on parsing non–standard strings applies. ;-) – RobG Mar 18 '19 at 02:02
  • @RobG Thank you for converting my code into es6 syntax, I didn't do it initially to keep code close to OP's one – flppv Mar 18 '19 at 02:03