Current Time in Another Location/Timezone [JavaScript]

JavaScript conver time to another timezone

This article will demonstrate how to get the current time in another location/timezone in Javascript, with just one line of code.

The Simplest Pure-JavaScript Way

The below function converts a given date to a different timezone, and contains just 1 line of code.

function timezoneConvert(date, tzString) {
    return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));   
}

This function expects:

  • A date, as either a Date object or string
  • A string containing the name of the timezone to convert to

This function works by creating a new Date object in the required timezone from the incoming Date object, converting the incoming date from a string if necessary.

To use it to get the current time in another location, you would simple pass it a new Date object, which defaults to the current time:

var currentTime = new Date();
var currentTimeInAdelaide = timezoneConvert(currentTime, "Australia/Adelaide") 
console.log(currentTimeInAdelaide.toLocaleString());

To convert a specific date/time, a string can be passed:

var convertedDate = timezoneConvert("2022/04/25 09:30:00 +0000", "Australia/Adelaide") 
console.log(convertedDate.toLocaleString());

JavaScript Date Objects

The above code utilizes JavaScript Date objects – special objects which can be assigned to variables to represent dates. Date objects include the specific details for a moment in time, including:

  • The date
  • The time
  • The timezone

The Date object also includes methods for retrieving the UTC time for that specific moment, and methods for retrieving the timezone offset. This makes the Date object a versatile way to handle dates and times.

Using Moment.js

There is an easier way, however. Moment.js is a JavaScript library which makes dealing with date, times, and timezones, as well as time intervals incredibly easy.

Converting a timezone in Moment.js is as simple is using the Moment Timezone module:

var timeInNewYork = moment.tz("2033-05-01 12:30", "America/New_York");

Store Everything as UTC!

If you are storing dates and times in a database or file for later retrieval, it is worth storing everything as UTC and then converting to the users timezone when the information is displayed. This removes any possibility for ambiguity or confusion, and means that your application will be accurate no matter where the user is located. This is especially important for web applications where you may have people logging in from across the world and interacting with each other.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published.