1const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
2
3// Example
4diffDays(new Date('2014-12-19'), new Date('2020-01-01')); // 1839
1/* difference between date1 and date2 in days (date2 - date1) */
2/* date1 and date 2 are already javascript date objects */
3function dateDifference(date2, date1) {
4 const _MS_PER_DAY = 1000 * 60 * 60 * 24;
5
6 // Discard the time and time-zone information.
7 const utc1 = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate());
8 const utc2 = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate());
9
10 return Math.floor((utc2 - utc1) / _MS_PER_DAY);
11}
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));