Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 above times by using moment.js.

If it's not possible in moment.js then please let me know using by javascript.

Inputs:

var startTime="01:30:00 am"; var endTime="2:45:07 pm"; 

Expected Output:

1 hour and 15 minutes 
like image 307
Ramesh Rajendran Avatar asked Apr 20 '15 10:04

Ramesh Rajendran


People also ask

How do I get the difference between two time in JavaScript?

Calculate the time difference in hours and minutes with: floor((diff/60)); minutes =(diff%60); } else { var diff1 = getTimeDiff('24:00', '{time1}', 'm'); var diff2 = getTimeDiff('{time2}', '00:00', 'm'); var totalDiff = diff1+diff2; hours = Math.

What is Momentjs?

Moment. js is a stand-alone open-source JavaScript framework wrapper for date objects that eliminates native JavaScript date objects, which are cumbersome to use. Moment. js makes dates and time easy to display, format, parse, validate, and manipulate using a clean and concise API.

Should you still use Momentjs?

Moment. js is a fantastic time & date library with lots of great features and utilities. However, if you are working on a performance sensitive web application, it might cause a huge performance overhead because of its complex APIs and large bundle size.


2 Answers

Try code below

// 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 - https://jsfiddle.net/nil4you/gs69Lv5x/

like image 117
nirmal Avatar answered Sep 21 '22 16:09

nirmal


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";  } 
like image 41
Ramesh Rajendran Avatar answered Sep 20 '22 16:09

Ramesh Rajendran