Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get difference between 2 dates in JavaScript? [duplicate]

How do I get the difference between 2 dates in full days (I don't want any fractions of a day)

var date1 = new Date('7/11/2010'); var date2 = new Date('12/12/2010'); var diffDays = date2.getDate() - date1.getDate();  alert(diffDays) 

I tried the above but this did not work.

like image 881
chobo2 Avatar asked Jul 11 '10 22:07

chobo2


People also ask

Can we subtract two dates in JavaScript?

Here, first, we are defining two dates by using the new date(), then we calculate the time difference between both specified dates by using the inbuilt getTime(). Then we calculate the number of days by dividing the difference of time of both dates by the no. of milliseconds in a day that are (1000*60*60*24).

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

Here is one way:

const date1 = new Date('7/13/2010');  const date2 = new Date('12/15/2010');  const diffTime = Math.abs(date2 - date1);  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));   console.log(diffTime + " milliseconds");  console.log(diffDays + " days");

Observe that we need to enclose the date in quotes. The rest of the code gets the time difference in milliseconds and then divides to get the number of days. Date expects mm/dd/yyyy format.

like image 149
TNi Avatar answered Oct 01 '22 17:10

TNi


A more correct solution

... since dates naturally have time-zone information, which can span regions with different day light savings adjustments

Previous answers to this question don't account for cases where the two dates in question span a daylight saving time (DST) change. The date on which the DST change happens will have a duration in milliseconds which is != 1000*60*60*24, so the typical calculation will fail.

You can work around this by first normalizing the two dates to UTC, and then calculating the difference between those two UTC dates.

Now, the solution can be written as,

const _MS_PER_DAY = 1000 * 60 * 60 * 24;  // a and b are javascript Date objects function dateDiffInDays(a, b) {   // Discard the time and time-zone information.   const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());   const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());    return Math.floor((utc2 - utc1) / _MS_PER_DAY); }  // test it const a = new Date("2017-01-01"),     b = new Date("2017-07-25"),     difference = dateDiffInDays(a, b); 

This works because UTC time never observes DST. See Does UTC observe daylight saving time?

p.s. After discussing some of the comments on this answer, once you've understood the issues with javascript dates that span a DST boundary, there is likely more than just one way to solve it. What I provided above is a simple (and tested) solution. I'd be interested to know if there is a simple arithmetic/math based solution instead of having to instantiate the two new Date objects. That could potentially be faster.

like image 33
Shyam Habarakada Avatar answered Oct 01 '22 19:10

Shyam Habarakada