Categories
JavaScript Answers

How to convert hours and minutes to minutes with moment.js?

Spread the love

Sometimes, we want to convert hours and minutes to minutes with moment.js.

In this article, we’ll look at how to convert hours and minutes to minutes with moment.js.

How to convert hours and minutes to minutes with moment.js?

To convert hours and minutes to minutes with moment.js, we can use the duration and asMinutes methods.

For instance, we write:

const m = moment.duration(moment('10:10:10', 'HH:mm:ss').format("HH:mm")).asMinutes()
console.log(m)

We call moment.duration with a moment object formatted in HH:mm format to get the duration object.

Then we convert the duration to an object with the asMinutes methods.

Therefore, we get 610 as a the value of m.

Conclusion

To convert hours and minutes to minutes with moment.js, we can use the duration and asMinutes methods.

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 *