Categories
JavaScript Answers

How to add a duration to a moment object with moment.js and JavaScript?

Spread the love

Sometimes, we want to add a duration to a moment object with moment.js and JavaScript.

In this article, we’ll look at how to add a duration to a moment object with moment.js and JavaScript.

How to add a duration to a moment object with moment.js and JavaScript?

To add a duration to a moment object with moment.js and JavaScript, we can use the add method.

For instance, we write

const travelTime = moment().add(2, "hours").format("hh:mm A");

to call add with 2 and 'hours' to add 2 hours to the current date time and return the new date time.

Then we call format to return a time string with the time.

Conclusion

To add a duration to a moment object with moment.js and JavaScript, we can use the add 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 *