How to Convert Date to Another Timezone in JavaScript?
Last Updated :
06 Dec, 2024
Improve
Converting a date to another timezone in JavaScript means adjusting the local date and time to match the time in a different timezone. This ensures that the displayed time aligns with the selected timezone, often using built-in methods or external libraries.
1. Using Intl.DateTimeFormat() and format() Methods
Intl.DateTimeFormat() in JavaScript allows formatting dates according to a specific locale and timezone. By using the format() method, you can convert a date into a human-readable string, adapting it to the desired timezone and regional formatting conventions.
Syntax
intlDateObj = new Intl.DateTimeFormat('en-US', {
timeZone: "America/New_York"
});
usaTime = intlDateObj.format(date);
let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
console.log('Given IST datetime: ', date);
let intlDateObj = new Intl.DateTimeFormat('en-US', {
timeZone: "America/New_York"
});
let usaTime = intlDateObj.format(date);
console.log('USA date: ', usaTime);
Output
Given IST datetime: 2012-12-20T03:00:00.000Z USA date: 12/19/2012
2. Using toLocaleString() Method
The toLocaleString() method is used to return a string that formats the date according to the locale and options specified. It will convert the date on which the method is used from one timezone to another.
Syntax
usaTime = date.toLocaleString("en-US", {timeZone: "America/New_York"});
let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
console.log('Given IST datetime: ', date);
let usaTime = date.toLocaleString("en-US", {
timeZone: "America/New_York"
});
console.log('USA datetime: ', usaTime);
Output
Given IST datetime: 2012-12-20T03:00:00.000Z USA datetime: 12/19/2012, 10:00:00 PM