Hour difference between two times(HH:MM:SS a)in momentjs

后端 未结 11 2435
温柔的废话
温柔的废话 2020-12-03 03:02

I have two time without date

var startTime=\"12:16:59 am\";
var endTime=\"06:12:07 pm\";

I want to show the total hours in between the abov

11条回答
  •  借酒劲吻你
    2020-12-03 03:11

    try below code

    // start time and end time
    var startTime = moment("12:16:59 am", "HH:mm:ss a");
    var endTime = moment("06:12:07 pm", "HH:mm:ss a");
    
    // calculate total duration
    var duration = moment.duration(endTime.diff(startTime));
    
    // duration in hours
    var hours = parseInt(duration.asHours());
    
    // duration in minutes
    var minutes = parseInt(duration.asMinutes())%60;
    
    alert (hours + ' hour and '+ minutes+' minutes.');
    

    check fiddle here http://jsfiddle.net/nil4you/gs69Lv5x/

提交回复
热议问题