Get Date & Time For a Given Timezone with Javascript

javascript
Published on March 27, 2021

Date & time for a given timezone can be found by using the Date.toLocaleString() method. This method takes an IANA timezone name (such as "America/Chicago", "Asia/Kolkata" etc) as an optional parameter.

// datetime in "America/Chicago" timezone in the "en-US" locale
let chicago_datetime_str = new Date().toLocaleString("en-US", { timeZone: "America/Chicago" });

// "3/22/2021, 5:05:51 PM"
console.log(chicago_datetime_str);

The return value of Date.toLocaleString() is a Javascript datetime string in the given timezone. We can then create a new Date object from this string to get date & time in our required format — YYYY-MM-DD / YYYY-MM-DD hh:mm:ss etc.

Get Current Time For a Timezone In YYYY-MM-DD Format

// current datetime string in America/Chicago timezone
let chicago_datetime_str = new Date().toLocaleString("en-US", { timeZone: "America/Chicago" });

// create new Date object
let date_chicago = new Date(chicago_datetime_str);

// year as (YYYY) format
let year = date_chicago.getFullYear();

// month as (MM) format
let month = ("0" + (date_chicago.getMonth() + 1)).slice(-2);

// date as (DD) format
let date = ("0" + date_chicago.getDate()).slice(-2);

// date time in YYYY-MM-DD format
let date_time = year + "-" + month + "-" + date;

// "2021-03-22"
console.log(date_time);

Get Current Time From a Timestamp

// Javascript timestamp in milliseconds
let ts = 1581338765000;

let chicago_datetime_str = new Date(ts).toLocaleString("en-US", { timeZone: "America/Chicago" });

// rest of the code
In this Tutorial
    Comments
    Loading Comments