Get Date and Time for a Given Timezone in Node.js

nodejs
Published on February 10, 2020

The toLocaleString() method of the Date() object can be used to get date and time for a given IANA timezone name in Nodejs

The below Javascript code will give date & time for a given IANA timezone name.

  • get current date string in YYYY-MM-DD format
  • get current time string in hh:mm:ss format
  • get current date-time string in YYYY-MM-DD hh:mm:ss format
// Date object initialized as per New Zealand timezone. Returns a datetime string
let nz_date_string = new Date().toLocaleString("en-US", { timeZone: "Pacific/Chatham" });

// Date object initialized from the above datetime string
let date_nz = new Date(nz_date_string);

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

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

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

// hours as (HH) format
let hours = ("0" + date_nz.getHours()).slice(-2);

// minutes as (mm) format
let minutes = ("0" + date_nz.getMinutes()).slice(-2);

// seconds as (ss) format
let seconds = ("0" + date_nz.getSeconds()).slice(-2);

// date as YYYY-MM-DD format
let date_yyyy_mm_dd = year + "-" + month + "-" + date;
console.log("Date in YYYY-MM-DD format: " + date_yyyy_mm_dd);

// time as hh:mm:ss format
let time_hh_mm_ss = hours + ":" + minutes + ":" + seconds;
console.log("Time in hh:mm:ss format: " + time_hh_mm_ss);

// date and time as YYYY-MM-DD hh:mm:ss format
let date_time = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
console.log("Date and Time in YYYY-MM-DD hh:mm:ss format: " + date_time);

To get date-time from a given timestamp, the Date() constructor can be initialized from the timestamp.

// Javascript timestamps are specified in milliseconds
let ts = 1581338765000;

let nz_date_string = new Date(ts).toLocaleString("en-US", { timeZone: "Pacific/Chatham" });

// rest of the code
In this Tutorial