0

Is it possible with vanilla JS to display foreign user time with his timezone?

Example:

  • ME: I am located in Prague (GMT+1)
  • USER: Is located in New York (EDT)

Current time is const localTime = 20:53 GMT+1

Current USER time is const userTime = 15:53 EDT - this is what I want to display to me: USER's time is 15:53 EDT

When USER is logged in in the beginning. I store his timezone offset: const tzOffset = new Date().getTimezoneOffset() Which returns difference from UTC in minutes.

How to with vanilla js display userTime 15:53 EDT using this tzOffset?

I have tried DateTimeFormat but options.timezone does not accept that tzOffset. And new Date has just toLocaleString().

Is there any easy way?

Edit:

Maybe better example

I have chat and there is me and the USER. If I hover over the USER I want to display a tooltip with his time with his timezone. Only what I have on my client is Date.prototype.getTimezoneOffset().

it is easy to add Hours to my current new Date() and get his time. But it is hard to display his timezone - EDT, GMT+/-X etc..

Dawe
  • 562
  • 1
  • 9
  • 19
  • 1
    No. However you can parse the return from [*Date.prototype.toString*](https://262.ecma-international.org/#sec-date.prototype.tostring) which contains the local time as HH:mm:ss and offset as GMT±HHmm and *might* contain a timezone name or abbreviation in brackets. – RobG Nov 04 '21 at 20:15
  • 1
    The *offset* is useless, you need the *timezone name*. Then it's as simple as `new Date().toLocaleString("en-US", {timeZone: 'America/New_York'})`. – deceze Nov 06 '21 at 09:54

0 Answers0