I need to convert hours and minutes in minutes values. With pure JavaScript Date object I do the following:
var d = new Date();
var minutes = d.getHours() * 60 + d.getMinutes();
I've just switched to moment.js and looking for better solution likes the following:
var minutes = moment(new Date()).toMinutes()
Is there is something like this?
minute() Method is used to get the minutes from the current time or to set the minutes. moment(). minutes();
Moment JS allows displaying of date as per localization and in human readable format. You can use MomentJS inside a browser using the script method. It is also available with Node. js and can be installed using npm.
The add() method in moment. js adds time to a particular time. We can add more days, more hours, more weeks, more months, and so on.
We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console. log(parsed.
I think your best bet is to create a Duration
and then get the minutes using asMinutes
. This is probably clearer when describing an interval of time.
moment.duration().asMinutes()
Here is the reference in the docs.
For those seeking to get minutes from an existing Moment
object, you can first format it to whatever you need, then use duration
to get its duration in minutes:
moment.duration(myMomentObjectToConvert.format("HH:mm")).asMinutes()
You could use:
var m = moment(new Date());
var minutes = (m.hour()*60) + m.minute();
http://momentjs.com/docs/#/get-set/minute/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With