How could I easily convert using JavaScript a time interval from a timezone to another?
Eg 1: 04:45-05:15 (hh24:mm-hh24:mm) UTC+1 to UTC+2 -> 05:45-06:15.
Eg 2: Sun:23:30-Mon:03:00 (day:hh24:mm-day:hh24:mm) UTC+1 to UTC+2 -> Mon:00:30-Mon:04:30
I guess there's a better and simplier way using JavaScript functions than doing it by myself.
Thank you
You can use toLocaleString()
to change the timezone of a date object. First you need to convert the date to a timestamp, I don't know how you have your dates stored as you haven't provided code so I will assume the date to be Jan 1st for the example
const timestamp = new Date('1 Jan 2019 4:45:00 UTC');
Then you need to use toLocaleString()
to convert to the desired timezone, as you said UTC+2 I will use pass the timezone object UTC+2
as the timezone parameter with en-GB
as the language to return in
const convertedDate = new Date(timestamp).toLocaleString('en-GB', {timezone: 'UTC+2'});
More reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.