0

I have timestamps therefore indicating the UTC +0 time.

I want to get the CT time related to those timestamps considering that the code can run from anywhere in the world.

now I can simply add the -6 Hours offset to those UTC timestamps, but that would not consider the DST possible offset.

And if I check the DST as in this solution I would not consider that the code can be running in a place where the DST was observed while it was not observed in Chicago (CT) during the time the timestamp refers to (there is 3 weeks period, twice a year, where America has DST and Europe does NOT.. or the other way round).

So my question is: at a given date, how can I know the UTC offset for a Time Zone which is not the Local Time Zone?

EDIT: I tried to use the this solution using new Date(1558361280000).toLocaleString("en-US", {timeZone: "America/Chicago"}) it returns 5/20/2019, 9:08:00 AM when instead it should have returned 5/20/2019, 3:08:00 PM

user3755529
  • 1,050
  • 1
  • 10
  • 30
  • I think you'll find https://momentjs.com/ is the solution to almost all date related problems. – Rocky Sims May 20 '19 at 21:44
  • 1
    @RockySims - Yes, but you'd need the moment-timezone extension also. However, there are multiple other approaches, including built-in support via the `timeZone` option of `toLocaleString`, which are referenced in the linked duplicate. – Matt Johnson-Pint May 20 '19 at 21:45
  • 1
    @user3755529 - Don't think about it in terms of which offset to add or subtract. Instead, identify [the IANA time zone name](http://en.wikipedia.org/wiki/List_of_tz_database_time_zones) you need (such as `"America/Chicago"`) and apply it using an API that accepts those. – Matt Johnson-Pint May 20 '19 at 21:50
  • if I use `new Date(1558361280000).toLocaleString("en-US", {timeZone: "America/Chicago"})` it returns **5/20/2019, 9:08:00 AM** when instead it should have returned **5/20/2019, 3:08:00 PM** ...this is because I am in London, I guess I have to correct first to the UTC by using `.getTimezoneOffset()` ? – user3755529 May 20 '19 at 22:14
  • I don't think it works because whenever you use `new Date()` you get dependent on the Local machine. – user3755529 May 20 '19 at 22:17

0 Answers0