1let startDate = "2021-04-01";
2let date1 = new Date();
3let date2 = new Date(startDate);
4let timeInMilisec = date1.getTime() - date2.getTime();
5let daysBetweenDates = Math.ceil(timeInMilisec / (1000 * 60 * 60 * 24));
1// new Date("dateString") is browser-dependent and discouraged, so we'll write
2// a simple parse function for U.S. date format (which does no error checking)
3function parseDate(str) {
4 var mdy = str.split('/');
5 return new Date(mdy[2], mdy[0]-1, mdy[1]);
6}
7
8function datediff(first, second) {
9 // Take the difference between the dates and divide by milliseconds per day.
10 // Round to nearest whole number to deal with DST.
11 return Math.round((second-first)/(1000*60*60*24));
12}
13
14alert(datediff(parseDate(first.value), parseDate(second.value)));