Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 675
Erik Avatar asked May 04 '14 10:05

Erik


People also ask

How do you find time in minutes in a moment?

minute() Method is used to get the minutes from the current time or to set the minutes. moment(). minutes();

What is Moment () in JavaScript?

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.

What is Moment () add?

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.

How do you convert moments to time?

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.


3 Answers

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.

like image 81
Davin Tryon Avatar answered Nov 07 '22 03:11

Davin Tryon


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()
like image 27
Alexandre Lara Avatar answered Nov 07 '22 02:11

Alexandre Lara


You could use:

var m = moment(new Date());
var minutes = (m.hour()*60) + m.minute();

http://momentjs.com/docs/#/get-set/minute/

like image 10
Hitchcott Avatar answered Nov 07 '22 02:11

Hitchcott