7

I am working on a date comparison and I am trying to calculate and display the difference between two dates in a format of dates, hours, minutes... Date values are stored in the DB like:

EndDate : 2018-11-29 10:49:49.9396033
PurchaseDate: 2018-11-29 10:49:07.4154497

And in my Angular component, I have:

let result = new Date(res.endDate).valueOf() - new Date(res.purchaseDate).valueOf();

This leads to: 42524 which I am not sure what it represents.

I wonder what is the proper way to calculate the time difference between two dates and also how can I display the result in a proper and readable way.

Any help is welcome

veben
  • 19,637
  • 14
  • 60
  • 80
George George
  • 571
  • 3
  • 7
  • 18
  • 2
    My recommended way of handling date comparison would be through the use of a very respectable library called momentjs. Excellent and reliable tool for handling dates, doing comparisons and manipulation. I think it will save you a lot of time and hassle. – Molik Miah Nov 29 '18 at 12:23
  • 3
    You might want to checkout - http://momentjs.com/. – Ankit Nov 29 '18 at 12:23
  • 2
    @George George, by the way, that difference you've listed is the difference in milliseconds between the two date values you've got. – miqh Nov 29 '18 at 12:23
  • 2
    Some nice ways to do it with plain JS as well here - https://stackoverflow.com/questions/1787939/check-time-difference-in-javascript – Ankit Nov 29 '18 at 12:25
  • 1
    42524 is (49.939-07.415)sec*1000 so basically the difference in date is being displayed in milliseconds – sah1 Nov 29 '18 at 12:43
  • 1
    Possible duplicate of [Check time difference in Javascript](https://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – Abdul Rauf Nov 29 '18 at 12:51

3 Answers3

8

Working Example in codepen

 let endDate = new Date("2018-11-29 10:49:07.4154497");
 let purchaseDate = new Date("2018-11-29 10:49:49.9396033");
 let diffMs = (purchaseDate - endDate); // milliseconds
 let diffDays = Math.floor(diffMs / 86400000); // days
 let diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
 let diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
 console.log(diffDays + " days, " + diffHrs + " hours, " + diffMins + " 
 minutes");
Anas Rezk
  • 199
  • 1
  • 4
6

You can use the getTime() method to get the difference time in milliseconds

let time = purchaseDate.getTime() - endDate.getTime();

You can then format the date as you want with the DatePipe librairy : https://angular.io/api/common/DatePipe

veben
  • 19,637
  • 14
  • 60
  • 80
  • thank you for the reply. I did this approach but then I face "getTime() is not a function".. – George George Nov 29 '18 at 16:11
  • 1
    You can try to look at this article: https://expertcodeblog.wordpress.com/2017/12/12/typescript-error-gethours-is-not-a-function/ – veben Nov 29 '18 at 16:38
1

Well, there are pure javascript way of doing it like in Check time difference in Javascript

or you can reuse the efforts put in by engineers who delevloped moment.js. In moment.js, there is a concept called duration whose link is - https://momentjs.com/docs/#/durations/ and you can even find the difference in duration by referencing docs here - https://momentjs.com/docs/#/durations/diffing/

Raja Malik
  • 66
  • 6