days between two dates
xxxxxxxxxx
Ex: DayDiff('2023/03/23', '2023/04/23') will return 31
function DayDiff(Date1, Date2){
Date1 = new Date(Date1);
Date2 = new Date(Date2);
return Math.abs(
date1.getTime() - date2.getTime()
) / (1000 * 60 * 60 * 24) < 1 ? 0 : Math.round(diffInMs / (1000 * 60 * 60 * 24));
}
function return (int) 0 if today
xxxxxxxxxx
let today = new Date().toISOString().slice(0, 10)
const startDate = '2021-04-15';
const endDate = today;
const diffInMs = new Date(endDate) - new Date(startDate)
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
alert( diffInDays );
xxxxxxxxxx
function calculateDaysBetweenDates(date1, date2) {
var oneDay = 24 * 60 * 60 * 1000;
var date1InMillis = date1.getTime();
var date2InMillis = date2.getTime();
var days = Math.round(Math.abs(date2InMillis - date1InMillis) / oneDay);
return days;
}
xxxxxxxxxx
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");
xxxxxxxxxx
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2020-10-21"), new Date("2021-10-22"))
// Result: 366
xxxxxxxxxx
const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
// Example
diffDays(new Date('2014-12-19'), new Date('2020-01-01')); // 1839
xxxxxxxxxx
// JavaScript program to illustrate
// calculation of no. of days between two date
// To set two dates to two variables
var date1 = new Date("06/30/2019");
var date2 = new Date("07/30/2019");
// To calculate the time difference of two dates
var Difference_In_Time = date2.getTime() - date1.getTime();
// To calculate the no. of days between two dates
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
//To display the final no. of days (result)
console.log("Total number of days between dates <br>"
+ date1 + "<br> and <br>"
+ date2 + " is: <br> "
+ Difference_In_Days);
xxxxxxxxxx
<input id="first" value="10/26/2023"/>
<input id="second" value="10/30/2023"/>
Run code snippetHide results