Categories
JavaScript Answers

How to set time with date in moment.js and JavaScript?

Spread the love

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 thehoursandminutes` 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.

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 *