Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Get difference between two dates in days [duplicate]

Tags:

typescript

I need to get the difference between two dates in days.

Up to now, I've been able to get the difference between two dates, but not in days:

date1.getTime() - date2.getTime(); 

Any ideas?

like image 404
Jordi Avatar asked May 02 '17 10:05

Jordi


People also ask

How do you find the difference between two dates in days?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.

How do I compare dates in typescript?

Call the getTime() method on each date to get a timestamp. Compare the timestamp of the dates. If a date's timestamp is greater than another's, then that date comes after.

How do I find the difference between two dates in the same column?

To calculate the difference between two dates in the same column, we use the createdDate column of the registration table and apply the DATEDIFF function on that column. To find the difference between two dates in the same column, we need two dates from the same column.


2 Answers

var diff = Math.abs(date1.getTime() - date2.getTime()); var diffDays = Math.ceil(diff / (1000 * 3600 * 24));  
like image 66
mohammad Avatar answered Sep 30 '22 20:09

mohammad


I believe the shortest route is:

var diff = date1.valueOf() - date2.valueOf(); 
like image 27
Roy Dictus Avatar answered Sep 30 '22 18:09

Roy Dictus