Sometimes, we want to set time with date in moment.js and JavaScript.
In this article, we’ll look at how to set time with date in moment.js and JavaScript.
How to set time with date in moment.js and JavaScript?
To set time with date in moment.js and JavaScript, we use the set
method.
For instance, we write
const hours = 15;
const minutes = 32;
const date = moment("2022-05-21").set("hour", hours).set("minute", minutes);
to call “setwith the
hoursand
minutes` to set the hours and minutes.
The first argument is the unit and the 2nd argument is the value to set.
A new moment object is returned with the new time value.
Conclusion
To set time with date in moment.js and JavaScript, we use the set
method.