0

How can I to parse times from different time zones?
JavaScript can do it, as shown in the code above:

process.env.TZ = "Asia/Jerusalem" // supported on Node v13+
new Date("2022-01-23 10:00").toString() // 10:00 GMT+02 (Israel Standard Time)
new Date("2022-07-23 10:00").toString() // 10:00 GMT+03 (Israel Daylight Time)
process.env.TZ = "Australia/Melbourne"
new Date("2022-01-23 10:00").toString() // 10:00 GMT+11 (Australian Eastern Daylight Time)
new Date("2022-07-23 10:00").toString() // 10:00 GMT+10 (Australian Eastern Standard Time)

But, how can it be done in less hacky way then changing the process.env.TZ?

Clarification: The requirement is to convert local time to UTC, not the other way around.
Converting UTC to local is easy:

new Date().toLocaleString("iso", { timeStyle:"full", timeZone:"America/New_York" })
Dani-Br
  • 2,289
  • 5
  • 25
  • 32
  • Does this answer your question? [How can I convert string to datetime with format specification in JavaScript?](https://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript) – evolutionxbox Jan 24 '22 at 00:11
  • I need to deal with timezone and daylight time, not with different date formats. – Dani-Br Jan 24 '22 at 00:16
  • 1
    Check out date-fns. They have some helper functions for time zones: https://date-fns.org/v2.28.0/docs/Time-Zones – GenericUser Jan 24 '22 at 00:18
  • You might find [this answer](https://stackoverflow.com/a/65818776/9078341) helpful. While it doesn't address the `.env.TZ` directly, it does show how to deal with times from different timezones. – Randy Casburn Jan 24 '22 at 00:27
  • What @RandyCasburn suggest, converts UTC time to different timezones. I need the opposite - to **parse** date & time (as string) from different timezones. – Dani-Br Jan 24 '22 at 00:50

1 Answers1

1

If you just want the offset of a timezone, you can take the IANA name for your timezone and pass it through to the date format like so:

const getOffset = (date, timezone) => -new Date(date).toLocaleString([], {timeZone: timezone, timeZoneName: 'shortOffset'}).match(/(?<=GMT|UTC).+/)[0]*60;

console.log(getOffset("2022-01-23 10:00", 'Asia/Jerusalem'))
console.log(getOffset("2022-07-23 10:00", 'Asia/Jerusalem'))

console.log(getOffset("2022-01-23 10:00", 'Australia/Melbourne'))
console.log(getOffset("2022-07-23 10:00", 'Australia/Melbourne'))
skara9
  • 4,042
  • 1
  • 6
  • 21
  • I like your suggestion. Just a note: It will fail two days a year, when the clock gets changed. It doesn't matter to me, because I care only about the working hours. – Dani-Br Jan 24 '22 at 01:11
  • The reason it will fail twice a year is that your code is checking the offset of time, being parsed as UTC. Example: `new Date("20 March 2022 01:00")` will be 1:00 at UTC, after the clock changed in Israel. But 1:00 in Israel time, is before the clock get changed. – Dani-Br Jan 24 '22 at 01:20
  • Makes sense, does node give a different value for this? – skara9 Jan 24 '22 at 01:28
  • Node do it correctly. Try: `process.env.TZ = "Australia/Melbourne";` then local time `new Date("2022-04-02T16:00").toString()` (Daylight Time), and UTC time `new Date("2022-04-02T16:00Z").toString()` (Standard Time). – Dani-Br Jan 24 '22 at 01:47