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

104,652

Solution 1

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

}

Solution 2

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/

Solution 3

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

Solution 4

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

Solution 5

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>

Share:
104,652
Ramesh Rajendran
Author by

Ramesh Rajendran

Technologies : HTML CSS Asp.Net MVC Web Api C# Angular 1-7 Unit Test (Front End &amp; Back End) I am currently working in L&amp;T . Read my blog C# DotNet Solutions.

Updated on July 09, 2022

Comments

  • Ramesh Rajendran
    Ramesh Rajendran almost 2 years

    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
    
  • Vigneswaran Marimuthu
    Vigneswaran Marimuthu about 9 years
    Moment constructor like this is discouraged and will be removed in future. Try to use with format :)
  • Ramesh Rajendran
    Ramesh Rajendran about 9 years
    It's return hour only, not minutes :( .
  • Vigneswaran Marimuthu
    Vigneswaran Marimuthu about 9 years
    @RameshRajendran You asked total hours in the question. So, what's your exact expectation ?
  • Vigneswaran Marimuthu
    Vigneswaran Marimuthu about 9 years
    @RameshRajendran I don't think moment has inbuilt support for this
  • Vigneswaran Marimuthu
    Vigneswaran Marimuthu about 9 years
    @RameshRajendran Check this one - stackoverflow.com/questions/1787939/…
  • Nico Haase
    Nico Haase about 6 years
    That's really ugly code... is there anything your code does better than the other?
  • Sandeep
    Sandeep over 5 years
    var startTime=moment("09:00:00 pm", "HH:mm:ss a"); var endTime=moment("04:00:00 am", "HH:mm:ss a"); var duration = moment.duration(endTime.diff(startTime)); var hours = parseInt(duration.asHours()); var minutes = parseInt(duration.asMinutes())%60; alert (hours + ' hour and '+ minutes+' minutes.'); Ouput is -17 hours. But it should have been 7 hours. Can you please tell me if i am doing something wrong
  • nirmal
    nirmal over 5 years
    yes I saw that, it's happening when we put pm before am. Mostly people do calculate only when it's day changed. I have found solution but it's not a robust. Once I have robust solution probably by today, then I will update my answer.
  • Tyler V
    Tyler V over 5 years
    The question was how can you do this in momentjs, this is how and its in one line.
  • jzacharuk
    jzacharuk about 5 years
    This is the only answer that allows easily changing the format (e.g. HH:mm:ss to mm:ss)
  • Ahmed Ali
    Ahmed Ali almost 3 years
    Any explanation would help viewer a lot.
  • Adrian Mole
    Adrian Mole almost 3 years
    Your code snippet doesn't work. Maybe just use code fences rather than pretend it's a runnable snippet?