How to change time with moment.js and JavaScript?

Sometimes, we want to change time with moment.js and JavaScript.

In this article, we’ll look at how to change time with moment.js and JavaScript.

How to change time with moment.js and JavaScript?

To change time with moment.js and JavaScript, we can use the set method.

For instance, we write:

const m = moment('2022-01-01', 'YYYY-MM-DD')
m.set({
  h: 11,
  m: 11
});
console.log(m)

to create a moment object from a date string with moment.

Then we call set on the returned moment object with an object that sets the hour h and minute m.

Therefore, the new value of the moment object is Jan 01 2022 11:11:00.

Conclusion

To change time with moment.js and JavaScript, we can use the set method.