var output = 1656277200000;
I also have the user that selects a timezone for example user selects London (GMT 1).
var timezone = "Europe/London";
I want to convert that output unix time from 27 June 2022 12:00:00 am GMT 3 to the timezone of what the user selected, the London timezone: 27 June 2022 12:00:00 am GMT 1. That means the unix: 1656284400000
CodePudding user response:
You would have to convert the timezone to an integer representing GMT. Then you subtract your timezone from theirs. You can convert the difference to milliseconds and add it to the original unix time. Example if your timezone is 5 and theirs is 7:
const myTimezone = 5;
const theirTimezone = 7;
const diffarance = theirTimezone - myTimezone; // set to 2
const differenceMilliseconds = diffarance * 3600000;
const theirUnix = myUnix differenceMilliseconds;
This also works if the the difference is negative.
CodePudding user response:
You can try this way
// Without moment-timezone
new Date().toLocaleString("en-US", { timeZone: "America/New_York" });
// With moment-timezone
var moment = require("moment-timezone");
const today = new Date();
var timeGet = moment(today);
timeGet.tz("Asia/Karachi").format("ha z");