How to Convert Date to Another Timezone in JavaScript

Here are three ways to convert a date to another timezone in JavaScript:

  1. Using Intl.NumberFormat() method
  2. Using toLocaleString() method
  3. Using moment-timezone library

Method 1: Using Intl.NumberFormat() method

To convert a date to another timezone in JavaScript, you can “use the Intl.DateTimeFormat object”, which is part of the ECMAScript Internationalization API. This allows you to format a date according to the conventions of a given locale, including adjusting for the local time zone.

Here’s a step-by-step guide on how to use it:

  1. Create a Date object in JavaScript.
  2. Use Intl.DateTimeFormat to format the date for a specific timezone.

Syntax

intlDateObj = new Intl.DateTimeFormat('en-US', {
                timeZone: "America/New_York"
              });

nycTime = intlDateObj.format(date);

Example

const dateInUTC = new Date();

const toTimeZone = (date, timeZone) => {
  return new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    timeZone: timeZone
 }).format(date);
}

const dateInNewYork = toTimeZone(dateInUTC, 'America/New_York');

console.log(dateInNewYork); 

Output

08/26/2023, 11:37:13 PM

The above code will convert the current UTC time to New York time. You can replace ‘America/New_York’ with any valid IANA timezone to convert to that timezone.

Method 2: Using toLocaleString() method

The toLocaleString() method is another way to format dates in JavaScript, and it can also adjust for time zones.

Here’s how to use toLocaleString() to convert a date to a different timezone:

  1. Create a Date object in JavaScript.
  2. Use the toLocaleString() method of the Date object to format the date for a specific timezone.

Example

const dateInUTC = new Date();

const dateInNewYork = dateInUTC.toLocaleString('en-US',
                        { timeZone: 'America/New_York' });

console.log(dateInNewYork);

Output

8/26/2023, 11:34:03 PM

Method 3: Using moment-timezone library

If you already use moment.js for date and time operations, you can easily leverage the moment-timezone extension to handle time zones. This extension provides comprehensive time zone support by building on top of moment.js.

Here’s how to use moment-timezone to convert dates between time zones:

  1. First, ensure you’ve included moment.js and moment-timezone in your project.
  2. Convert dates between time zones using the .tz() function.

Example

Install the moment-timezone library using the “NPM”:

npm install moment-timezone --save

Using moment-timezone library

Here’s an example:

const moment = require('moment-timezone');

let dateInUTC = moment.utc('2023-08-27T12:00:00Z');

let dateInNewYork = dateInUTC.tz('America/New_York');

console.log(dateInNewYork.format());

Output

2023-08-27T08:00:00-04:00

You can also create dates directly in a specific time zone:

const moment = require('moment-timezone');

let localDateInTokyo = moment.tz('2023-08-27 12:00:00', 'Asia/Tokyo');

console.log(localDateInTokyo.format());

Output

2023-08-27T12:00:00+09:00

With moment-timezone, you can easily manipulate, format, and convert dates across different time zones. It’s beneficial if you’re familiar with moment.js and want to extend its capabilities to handle time zones effectively.

That’s it!

Related posts

Convert GMT to local time in JavaScript

Convert Timestamp to Date in JavaScript

Convert Unix Timestamp to Time in JavaScript

How to Convert a Date to UTC

Convert Date to String in JavaScript

Leave a Comment