javascript count days between dates code example

Example 1: js get difference in days

const date1 = new Date('19/1/2021');
const date2 = new Date('20/1/2021');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");

Example 2: get the difference between two dates js

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");

Example 3: how to calculate the number of days between two dates in javascript

function javascript (date1, date2) {
   let oned = 24 * 60 * 60 * 1000;
   return Math.ceil((date2 - date1) / oned);
}

function javascript(date1, date2) {
	return new Date(date2 - date1).getDate() - 1
}

function javascript(date1, date2) {
  return Math.ceil((date2 - date1) / 8.64e7);
}