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

后端 未结 11 2415
温柔的废话
温柔的废话 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:08

    I know this is marked as already answered but you can literally just do...

    moment.utc(moment.duration(moment(dateA).diff(moment(dateB))).asMilliseconds()).format('HH:mm:ss');
    
    0 讨论(0)
  • 2020-12-03 03:10
    var start = moment.duration("09:45", "HH:mm");
    var end = moment.duration("10:30", "HH:mm");
    var diff = end.subtract(start);
    diff.hours(); // return hours
    diff.minutes(); // return minutes
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-12-03 03:19

    Get Hours

    I got the hours by using this code

    endTime.diff(startTime, 'hours')
    

    Get Minutes

    i got the minutes by using this below code

    var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")
    

    My Working code is

    $scope.UpdateTimeSheet = function (rowEntity) {   
    
    
                if (rowEntity.StartTime.toString().length != 11) {
                    rowEntity.StartTime = moment(rowEntity.StartTime).format("hh:mm:ss a");
                }
                if (rowEntity.EndTime.toString().length != 11) {
                    rowEntity.EndTime = moment(rowEntity.EndTime).format("hh:mm:ss a");
                }
    
                var startTime = moment(rowEntity.StartTime, "hh:mm:ss a");
                var endTime = moment(rowEntity.EndTime, "hh:mm:ss a");
    
                var mins = moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")
    
                rowEntity.TotalHours = endTime.diff(startTime, 'hours') + " Hrs and " + mins + " Mns";
    
    }
    
    0 讨论(0)
  • 2020-12-03 03:19
    //Get start time and end time
    var startTime = moment("12:16:59 am", "HH:mm:ss a"),
    endTime = moment("06:12:07 pm", "HH:mm:ss a");
    
    1. Method 1

    var dif = moment.duration(endTime.diff(startTime));
    console.log([dif.hours(), dif.minutes(), dif.seconds()].join(':'));
    console.log('dif in Mins: ', (dif.hours() * 60) + dif.minutes());
    
    1. Method 2

    console.log('hrs: ', moment(endTime).diff(startTime, 'hours'));
    console.log('dif in Mins: ', moment(endTime).diff(moment(startTime), 'minutes'));
    
    1. Method 3

    var hrs = moment.utc(endTime.diff(startTime)).format("HH");
    var min = moment.utc(endTime.diff(startTime)).format("mm");
    var sec = moment.utc(endTime.diff(startTime)).format("ss");
    console.log([hrs, min, sec].join(':'));
    
    1. Formatted output

    var dif = moment.duration(endTime.diff(startTime));
    console.log(dif.humanize());
    
    0 讨论(0)
  • 2020-12-03 03:19

    For - "12:00:01" Format without am, pm format following os the code..

       var startTime = moment('12:00:01', 'hh:mm:ss a');
       var endTime = moment('13:00:10' , 'hh:mm:ss a');
       var totalHours = (endTime.diff(startTime, 'hours'));
       var totalMinutes = endTime.diff(startTime, 'minutes');
       var clearMinutes = totalMinutes % 60;
       alert(totalHours + " hours and " + clearMinutes + " minutes");
    
    0 讨论(0)
提交回复
热议问题