admin管理员组

文章数量:1135165

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

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
Share Improve this question edited Jul 17, 2018 at 6:46 Ramesh Rajendran asked Apr 20, 2015 at 10:53 Ramesh RajendranRamesh Rajendran 38.6k49 gold badges157 silver badges237 bronze badges 2
  • possible duplicate of Get hours difference between two dates in Moment Js – Shaunak D Commented Apr 20, 2015 at 10:58
  • 1 @ShaunakD duplicate is not accetable cause will return asTOTAL values – Paolo Falomo Commented Jun 16, 2017 at 9:31
Add a comment  | 

14 Answers 14

Reset to default 80

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/

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');
//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());

myStart = "01:30:00 am";
myEnd = "2:45:07 am";

function getTimeDiff(start, end) {

  return moment.duration(moment(end, "HH:mm:ss a").diff(moment(start, "HH:mm:ss a")));
}

diff = getTimeDiff(myStart, myEnd)
console.log(`${diff.hours()} Hour ${diff.minutes()} minutes`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

This worked for me; the best solution so far

function calculateTimeDifference() {
    var date1 = new Date();
    var date2 = new Date();

    var diff = date2.getTime() - date1.getTime();

    var msec = diff;
    var hh = `0${Math.floor(msec / 1000 / 60 / 60)}`;
    msec -= hh * 1000 * 60 * 60;

    var mm = `0${Math.floor(msec / 1000 / 60)}`;
    msec -= mm * 1000 * 60;

    var ss = `0${Math.floor(msec / 1000)}`;
    msec -= ss * 1000;

    return hh.slice(-2) + ":" + mm.slice(-2) + ":" + ss.slice(-2); 
}
var startTime = moment("12:16:59 am", 'hh:mm:ss a');
var endTime = moment("06:12:07 pm", 'hh:mm:ss a');

var totalHours = (endTime.diff(startTime, 'hours'));
var totalMinutes = endTime.diff(startTime, 'minutes');
var clearMinutes = totalMinutes % 60;
console.log(totalHours + " hours and " + clearMinutes + " minutes");
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

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');
 const end = moment(event.end, "HH:mm:ss")
 const start = moment(event.start, "HH:mm:ss")
 const dif = moment.duration(end.diff(start));
 const formattedDiff = dif.minutes() > 0 ? `${(dif.hours() + dif.minutes() / 60)} hrs` : `${dif.hours()} hrs`
// Result: 1.5 hrs / 2 hrs / 4.25 hrs

I came up with this solution to show up difference in hour(s) (minutes converted, you can use Math.floor for minutes maybe but my case the minutes are always half/quarter)

I got it from @Vigneswaran Marimuthu

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

var result = endTime.diff(startTime, 'hours') + " Hrs and " +     
            endTime.diff(startTime, 'minutes') + " Mns";

If we want to get seconds, then you just rewrite the code "seconds" instead of hours

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

var fromtime = moment($('#fromtime').val(), "HH:mm");
var totime = moment($('#totime').val(), "HH:mm");
var dif = moment.duration(totime.diff(fromtime));
var diff = [dif.hours(), dif.minutes()].join(':');
if(diff != "NaN:NaN" && dif.hours()>=0 && dif.minutes()>=0){
    $('#duration').text([dif.hours(), dif.minutes()].join(':'));
}else{
    $('#duration').text('');
}

var startTime = moment("12:16:59");
var endTime = moment("06:12:07");

var duration = moment.duration(endTime.diff(startTime));
var hours = duration.asHours();

本文标签: javascriptHour difference between two times(HHMMSS a)in momentjsStack Overflow