DEV Community

Cover image for Dynamically change timestamp based on time zone in JavaScript
Hardique Dasore
Hardique Dasore

Posted on • Edited on

Dynamically change timestamp based on time zone in JavaScript

In order to dynamically change timestamp based on time zone in JavaScript you first need to get your system’s IANA time zone in JavaScript using:

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
Enter fullscreen mode Exit fullscreen mode

Once you have your system’s time zone, you need to assign the value of timestamp to a variable. The time stamp can be of any format, the following code is using ISO date format which is commonly used by Azure B2C, etc. In the next step we need to convert the date to a string using toLocaleString() method which helps in the sensitive representation of the date. You can now use the string to change the time stamp to your time zone.

var timeStamp = new Date("2011-08-12T20:17:46.384Z"); 
//ISO date format
console.log(timeStamp.toLocaleString('en-US', { 
   timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone 
}))
Enter fullscreen mode Exit fullscreen mode

Use case: If you have a global web application and you want to display time stamp of account creation to the application administrator.

Please make sure that the date is in YYYY-MM-DDTHH:MM:SS.XXXZ format

Image description

Follow us on Instagram

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay