Search code examples
javascriptdatedatediff

Javascript calculate difference between two dates yy-mm-dd


I've read quite a few articles and questions on calculating date differences in javascript, but I can't find them for the format I'm looking for.

I don't need hours, minutes or believe it or not milliseconds... I just need days. I'm checking to be sure one day occurs on the same day or after another.

Date format is 2010-10-05

I tried this, but all I get is NaN:

var diff =  Math.floor(( Date.parse(end_date) - Date.parse(start_date) ) / 86400000);

Solution

  • Do I understand correctly that you don't actually need to know how many days apart the two days are, you just need to know if they're the same date vs. if one is a later date? In that case, regular string comparisons can tell you that, provided you consistently use the format 'yyyy-mm-dd', with two-digit months and two-digit days; for example, '2010-10-05' < '2010-10-16'.