I need to get time difference in hours and minutes of other user who is situated in anather country. Here is what i have done.
const timeZone = "Asia/tokyo"; // Time zone can be changed
let arr: any = new Date().toLocaleString("en-US", {
timeZone: timeZone,
dateStyle: "full",
timeStyle: "full",
});
let currentTime = new Date();
Need to get difference between currentHour and arr
2
Answers
Firstly, use
en-CA
locale for the "calculation" … it outputsyyyy-mm-dd hh:mm:ss
which makes it simple to manipulateSecondly, add
hour12: false
as you want 24 hour timeThen you can
My Typescript is inelegant … but
Tried that on a TS Playground, seems to work
Solutions like that from Jaromanda X (reproduced below using Intl.DateTimeFormat.formatToParts) are OK, however better to pass an actual Date object rather than the date and time values.
I think it’s more elegant to get the difference between the actual offsets (see lower).
Using timezone offsets: