NodeJs – How to get the current time for a specific timezone

PROBLEM: How to get the current time for a specific timezone?

SOLUTION: The following code will give you access to the current time in ‘America/Los_Angeles’. You can get the date/time in any timezone.

let currentDate = new Date()
let currentDateStr = currentDate.toLocaleString('en-US', { timeZone: 'America/Los_Angeles' });
let newCurrentDate = Date.parse(currentDateStr);
let hours = new Date(newCurrentDate).getHours();

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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