This question already has an answer here:
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.
Good Answer: 👌👌👌
var date1 = new Date("7/13/2010"); var date2 = new Date("12/15/2010"); var timeDiff = Math.abs(date2.getTime() - date1.getTime()); var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); alert(diffDays);
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.