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

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


try below code

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

check fiddle herehttp://jsfiddle.net/nil4you/gs69Lv5x/


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";}


var startTime = moment("12:16:59 am", 'hh:mm:ss a');var endTime = moment("06:12:07 pm", 'hh:mm:ss a');endTime.diff(startTime, 'hours');