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

后端 未结 11 2463
温柔的废话
温柔的废话 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: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";
    
    }
    

提交回复
热议问题