Using moment.js how do you convert a moment object to minutes?

Viewed 250

There is a startTime being set from the innerText of an element. This string is then converted to a Moment.js object using moment(startTime). How is this then converted into a number value as minutes?

Javascript

startTime = document.querySelector(".start-time").text;
startTime = moment(startTime);
console.log(startTime)

console.log

Moment {_isAMomentObject: true, _i: "08:03:03 PM", _isUTC: false, _pf: {…}, _locale: Locale, …}
    _d: Invalid Date {}
    _i: "08:03:03 PM"
    _isAMomentObject: true
    _isUTC: false
    _isValid: false
    _locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …}
    _pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
    __proto__: Object

Update (solution): for anyone who might find helpful

startTime = moment.duration(startTime).asMinutes();
1 Answers

There are many ways to convert time into minutes:

CASE 1

I didn't needed moment.js, at least worked for my scenarios:

console.log('converting 2 hours, 20 minutes and 120 seconds to minutes');

var hours = 3,
  minutes = 10,
  seconds = 150,
  timeInminutes = (hours * 60) + minutes + (seconds / 60);

console.log('2 hours, 20 minutes and 120 seconds to minutes is = ', timeInminutes, ' minutes')

CASE 2

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();

CASE 3

You could use:

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