bguiz bguiz - 1 year ago 425
Javascript Question

Minutes since midnight in Momentjs

In pure JS, this would be how.

How can I find out the number of minutes since midnight for a given

object (without extracting to

  • Must take into account DSTs

  • Minutes should be rounded

  • Must work with local time (not convert to UTC)

Answer Source
// Your moment
var mmt = moment();

// Your moment at midnight
var mmtMidnight = mmt.clone().startOf('day');

// Difference in minutes
var diffMinutes = mmt.diff(mmtMidnight, 'minutes');

By default, moment#diff will return number rounded down. If you want the floating point number, pass true as the third argument. Before 2.0.0, moment#diff returned rounded number, not a rounded down number.

Consider this pseudocode because I haven't test to see if the difference takes into account DST.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download