Retrieve Date & Time For A Specific Timezone Using Javascript

By utilizing the Date.toLocaleString() function, one may discover the date and time for a certain timezone. This function accepts an optional parameter that is the name of an IANA timezone, such as “America/Chicago,” “Asia/Kolkata,” etc.

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

Date.toLocaleString() returns a Javascript DateTime string in the specified timezone as its return value. Then, we can build a new Date object from this string to obtain the date and time in the format we need: YYYY-MM-DD. I.e., hh:mm:ss

YYYY-MM-DD Format Current Time For A Timezone

Current DateTime string in the Indian timezone

let datetime = new Date().toLocaleString("en-US", { timeZone: "ist" });

Create a new Date object

let date = new Date(datetime);

Year as (YYYY) format

let year = date.getFullYear();

Month as (MM) format

let month = ("0" + (date.getMonth() + 1)).slice(-2);

Date as (DD) format

let datedd = ("0" + date.getDate()).slice(-2);

Date time in YYYY-MM-DD format

let date_time = year + "-" + month + "-" + datedd;

Get Current Time From a Timestamp

let ts = 1581338765000;

let datetime_str = new Date(ts).toLocaleString("en-US", { timeZone: "ist" });

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories