1let today = new Date().toISOString().slice(0, 10)
2
3const startDate = '2021-04-15';
4const endDate = today;
5
6const diffInMs = new Date(endDate) - new Date(startDate)
7const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
8
9
10alert( diffInDays );
1function calculateDaysBetweenDates(date1, date2) {
2 var oneDay = 24 * 60 * 60 * 1000;
3 var date1InMillis = date1.getTime();
4 var date2InMillis = date2.getTime();
5 var days = Math.round(Math.abs(date2InMillis - date1InMillis) / oneDay);
6 return days;
7}
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}
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
1const date1 = new Date('19/1/2021');
2const date2 = new Date('20/1/2021');
3const diffTime = Math.abs(date2 - date1);
4const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
5console.log(diffTime + " milliseconds");
6console.log(diffDays + " days");
1<input id="first" value="25/2/2021"/>
2<input id="second" value="26/2/2021"/>