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.