Get hours difference between two dates in Moment Js
You were close. You just need to use the duration.asHours()
method (see the docs).
var duration = moment.duration(end.diff(startTime));
var hours = duration.asHours();
Hour difference between two times(HH:MM:SS a)in momentjs
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";
}
Get the time difference between two datetimes
This approach will work ONLY when the total duration is less than 24 hours:
var now = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";
moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")
// outputs: "00:39:30"
If you have 24 hours or more, the hours will reset to zero with the above approach, so it is not ideal.
If you want to get a valid response for durations of 24 hours or greater, then you'll have to do something like this instead:
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
// outputs: "48:39:30"
Note that I'm using the utc time as a shortcut. You could pull out d.minutes()
and d.seconds()
separately, but you would also have to zeropad them.
This is necessary because the ability to format a duration
objection is not currently in moment.js. It has been requested here. However, there is a third-party plugin called moment-duration-format that is specifically for this purpose:
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = d.format("hh:mm:ss");
// outputs: "48:39:30"
How to get difference between two datetime of moment.js?
I found the solution ,may help other people :
var now = moment(new Date(), "DD/MM/YYYY HH:mm:ss");
var then = moment(new Date(startDate), "DD/MM/YYYY HH:mm:ss");
var diffTime = moment.duration(then.diff(now));
and it depends to you how you want to get difference in seconds ,minutes or hours :
diffTime.asHours();
Difference between two dates moment.js
$('#test').click(function() { var startDate = moment("13/04/2016", "DD/MM/YYYY"); var currenDate = moment(new Date()).format("DD/MM/YYYY"); var endDate = moment(currenDate, "DD/MM/YYYY"); var result = 'Diff: ' + endDate.diff(startDate, 'days'); $('#result').html(result);});
#test { width: 100px; height: 100px; background: #ffb; padding: 10px; border: 2px solid #999;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>
<div id='test'>Click Me!!!</div><div id='result'></div>
Moment.js - two dates difference in number of days
From the moment.js docs: format('E')
stands for day of week. thus your diff is being computed on which day of the week, which has to be between 1 and 7.
From the moment.js docs again, here is what they suggest:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
Here is a JSFiddle for your particular case:
$('#test').click(function() { var startDate = moment("13.04.2016", "DD.MM.YYYY"); var endDate = moment("28.04.2016", "DD.MM.YYYY");
var result = 'Diff: ' + endDate.diff(startDate, 'days');
$('#result').html(result);});
#test { width: 100px; height: 100px; background: #ffb; padding: 10px; border: 2px solid #999;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.js"></script>
<div id='test'>Click Me!!!</div><div id='result'></div>
How can I get the hour difference between dates within 24 hours with moment.js?
If you are looking for all times to be in hours, then the answer from stdob-- is fine.
It is worth noting that Moment has .fromNow()
built in. Simply take the date string for when your item was created, pass that to the moment constructor, and call the function:
moment($created).fromNow();
As a practical example:
moment("2016-05-01T09:58:26.796Z").fromNow()
"2 hours ago"
or, for longer ago:
moment("2016-04-28T09:58:26.796Z").fromNow()
"3 days ago"
This function will give a little more user friendly output, going from 'a few seconds ago' to 'x minutes ago' to 'y hours ago' and into days, months and years.
The output text of fromNow can be customized. See http://momentjs.com/docs/#/displaying/fromnow/ for the basic documentation and http://momentjs.com/docs/#/customization/relative-time/ for customization instructions.
As a final best practice note, it should never be necessary to use a javascript Date object with Moment.js unless you have a third party API that requires a date. For what you are doing though, no Date needed.
Get the time difference in hh:mm between two datetimes using moment
Based on the previous answer given, I was able to workaround on same logic and get the correct solution for my question.
let start moment(startTimeStampInMS),
end = moment(endTimeStampInMS);
let timediffms = end-start; // difference in ms
let duration = moment.duration(timediffms);
// _data should not be used because it is an internal property and can
//change anytime without any warning
//let hours = duration._data.days*24 + duration._data.hours;
//let min = duration._data.minutes;
let hours = duration.days()*24+ duration.hours();
let min = duration.minutes();
timeDifference = hours +":"+ min; // Answer
Related Topics
5Xx or 4Xx Error with "No 'Access-Control-Allow-Origin' Header Is Present"
What Is the Correct Way to Check for String Equality in JavaScript
How to Change Href of <A> Tag on Button Click Through JavaScript
Can JavaScript Connect with MySQL
Ajax Jquery Refresh Div Every 5 Seconds
Keycode on Android Is Always 229
How to Add and Remove Classes in JavaScript Without Jquery
How to Upload Image into HTML5 Canvas
Jquery (Almost) Equivalent of PHP's Strip_Tags()
How to Automatically Set the Focus to a Textbox When a Web Page Loads
How to Chop/Slice/Trim Off Last Character in String Using JavaScript
What Are the Differences Between Deferred, Promise and Future in JavaScript
JavaScript Require() Function Giving Referenceerror: Require Is Not Defined
How to Check If an HTML Element Is Empty Using Jquery
Removing # from Url in Angularjs While Having .Run in Routes
\U200B (Zero Width Space) Characters in My Js Code. Where Did They Come From
How to Check If Geolocation Has Been Declined with JavaScript