Open In App

How to set timezone offset using JavaScript ?

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The timezone offset represents the difference in minutes between local time and Coordinated Universal Time (UTC). Here are two different ways to set the timezone offset in JavaScript.

1. Using getTimezoneOffset() Method

The getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, the local timezone is behind the UTC and if it is negative, the local timezone is ahead of UTC. The returned value is not constant if the host system is configured for daylight saving.

JavaScript
// Date object
let date = new Date();

// Offset variable will store 
// timezone offset between 
// UTC and your local time   
let offset = date.getTimezoneOffset();

console.log("Timezone offset: ", offset, " minutes");

Output
Timezone offset:  0  minutes

Note: The method returns your local timezone offset in minutes and not the timezone offset of the “date” object.

2. Using Intl.DateTimeFormat() with Timezone

The Intl.DateTimeFormat with the timeZone option allows formatting dates according to a specific timezone for display purposes. This method doesn’t alter the underlying date object but formats the date string to reflect the specified timezone, providing a locale-sensitive way to present time.

JavaScript
// Create Date object for current date/time
let date = new Date();

// Format date for 'America/New_York' timezone
let formattedDate = new Intl.DateTimeFormat('en-US', {
    timeZone: 'America/New_York',  // Set timezone
    year: 'numeric',               // Show year
    month: '2-digit',              // Show month
    day: '2-digit',                // Show day
    hour: '2-digit',               // Show hour
    minute: '2-digit',             // Show minute
    second: '2-digit',             // Show second
    hour12: true                   // Use 12-hour format
}).format(date);

// Log formatted date
console.log(`Formatted Date (New York Time): ${formattedDate}`);

Output
Formatted Date (New York Time): 12/05/2024, 09:11:42 PM


Next Article

Similar Reads