Categories
JavaScript Answers

How to fix UTC gives wrong date with moment.js and JavaScript?

Spread the love

To fix UTC gives wrong date with moment.js and JavaScript, we should add the time zone to the datetime string we’re parsing into a moment object.

For instance, we write

const datetime = moment(new Date("07-18-2022 UTC"))
  .utc()
  .format("YYYY-MM-DD HH:mm");

to call moment with the new Date("07-18-2022 UTC") date, which we specified to be UTC.

Then we call utc to return a UTC moment.

Next, we call format to return a datetime string in specified format.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.