Minutes since midnight in Momentjs

前端 未结 2 1279
野的像风
野的像风 2021-01-03 18:42

In pure JS, this would be how.

How can I find out the number of minutes since midnight for a given moment object (without extracting to Date

相关标签:
2条回答
  • 2021-01-03 19:20
    // 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.

    0 讨论(0)
  • 2021-01-03 19:21

    This is what I have at the moment:

        if (!moment.isMoment(mmt)) {
            return 0;
        }
        var hh = mmt.get('hour');
        var mm = mmt.get('minute');
        return hh*60 + mm;
    

    I am not sure if it takes into account various edge cases; comment if this is the case, or provide an alternate answer.

    0 讨论(0)
提交回复
热议问题