Categories
JavaScript Answers

How to display date/time in user’s locale format and time offset with JavaScript?

Spread the love

Sometimes, we want to display date/time in user’s locale format and time offset with JavaScript.

In this article, we’ll look at how to display date/time in user’s locale format and time offset with JavaScript.

How to display date/time in user’s locale format and time offset?

To display date/time in user’s locale format and time offset, we can use the DateTimeFormat object’s format method.

For instance, we write

const utcDate = new Date(Date.UTC(2022, 11, 20, 3, 23, 16, 738));
console.log(new Intl.DateTimeFormat().format(utcDate));

to create a UTC timestamp with Date.UTC.

And then we convert the timestamp to a DFate object with the Date constructor.

Next, we call format with utcDate to return a date string formatted to the user’s current locale.

Conclusion

To display date/time in user’s locale format and time offset, we can use the DateTimeFormat object’s format method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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