Categories
JavaScript Answers

How to convert Moment.js date to users local time zone with JavaScript?

Spread the love

Sometimes, we want to convert Moment.js date to users local time zone with JavaScript.

In this article, we’ll look at how to convert Moment.js date to users local time zone with JavaScript.

How to convert Moment.js date to users local time zone with JavaScript?

To convert Moment.js date to users local time zone with JavaScript, we can use the local method.

For instance, we write

const testDateUtc = moment.utc("2022-01-30 10:00:00");
const localDate = moment(testDateUtc).local();

to create a moment UTC datetime object with moment.utc.

Then we call moment with testDateUtc to create a new moment object.

Finally, we call local on it to return testDateUtc converted to the local time zone.

Conclusion

To convert Moment.js date to users local time zone with JavaScript, we can use the local 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 *