In my application backend is applying user timezone and sending formatted date to UI. But when I am converting that string to date object. It is converting to user's system timezone. How can I change date string to specific timezone moment object not to user system timezone.
I am using moment-timezone library.
For ex. 07/26/2022 07:01:14 AM is already in GMT-5 but when I am converting this to moment object using moment(new Date(dateString))
It is converting it to user system timezones.
How can I convert it to user defined timezone which is GMT-5
CodePudding user response:
I just whipped this up - not using momentjs, because it's no longer maintained
const input = "07/26/2022 07:01:14 AM"
const date= new Date(input);
// VVVVVV timezone fix
date.setMinutes(date.getMinutes() - date.getTimezoneOffset() 5 * 60);
// date is now "07/26/2022 07:01:14 AM GMT-5"
// the following is just to show it's right
const verifyDate = new Intl.DateTimeFormat(navigator.language, {
dateStyle: 'full',
timeStyle: 'full',
timeZone: 'America/Bogota' // just picking a GMT-5 zone at random
}).format(date);
console.log(verifyDate)
// at this point