最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript : find date difference in dateshoursminutes - Stack Overflow

programmeradmin9浏览0评论

I am working on a date parison 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 ponent, 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 wele

I am working on a date parison 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 ponent, 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 wele

Share Improve this question edited Nov 29, 2018 at 13:45 veben 22.3k15 gold badges69 silver badges83 bronze badges asked Nov 29, 2018 at 12:17 George GeorgeGeorge George 6013 gold badges8 silver badges18 bronze badges 6
  • 2 My remended way of handling date parison would be through the use of a very respectable library called momentjs. Excellent and reliable tool for handling dates, doing parisons and manipulation. I think it will save you a lot of time and hassle. – Molik Miah Commented Nov 29, 2018 at 12:23
  • 3 You might want to checkout - momentjs.. – Ankit Commented Nov 29, 2018 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 Commented Nov 29, 2018 at 12:23
  • 2 Some nice ways to do it with plain JS as well here - stackoverflow./questions/1787939/… – Ankit Commented Nov 29, 2018 at 12:25
  • 1 42524 is (49.939-07.415)sec*1000 so basically the difference in date is being displayed in milliseconds – sah1 Commented Nov 29, 2018 at 12:43
 |  Show 1 more ment

3 Answers 3

Reset to default 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");

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/mon/DatePipe

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./docs/#/durations/ and you can even find the difference in duration by referencing docs here - https://momentjs./docs/#/durations/diffing/

发布评论

评论列表(0)

  1. 暂无评论