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..