1

I have a date in string format e.g. 2020-07-02T20:00:00.000+10:00. It has a offset +10:00 in the end. How can I get the timezone name from this string?

Below code will give me the offset in seconds which is -600. How can I get the timezone name in string? The value should be 'Australia/Melbourne'.

> d=new Date('2020-07-02T20:00:00.000+10:00')
2020-07-02T10:00:00.000Z
> d.getTimezoneOffset()
-600

I'd like to use build-in function to achieve rather than 3rd part library.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • 1
    Problem 1, there is more than one timezone name with that offest – Jon P Jul 02 '21 at 05:53
  • No, it uses moment js which I don't want to use. – Joey Yi Zhao Jul 02 '21 at 05:56
  • [The top answer](https://stackoverflow.com/a/44935836/4665) does not use moment at all – Jon P Jul 02 '21 at 05:58
  • The top one returns a string like `Australian Eastern Standard Time` which is not what I want. I want to get the string `Australia/Melbourne` which can be used in `toLocaleDateString`. – Joey Yi Zhao Jul 02 '21 at 05:59
  • Why should the function return Australia/Melbourne and not Australia/Hobart or Asia/Vladivostok? Or what if Australia decides to observe DST and +10 becomes +9 or +11? – Salman A Jul 02 '21 at 06:04
  • Interesting , what browser? I get `Australia/Sydney` for my linked answer in FireFox, Chrome and Edge (All Win10) – Jon P Jul 02 '21 at 06:06
  • All of them are ok as long as it represents +10:00 offset. – Joey Yi Zhao Jul 02 '21 at 06:06
  • @JonP not in browser, it is in node.js environment. – Joey Yi Zhao Jul 02 '21 at 06:41
  • Fair enough, I've voted to re-open. I might suggest editing your question to highlight the fact this is for node (some times tags get glossed over) – Jon P Jul 02 '21 at 13:28

0 Answers0