xxxxxxxxxx
try to use
dayjs().diff();
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 );
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
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
// Returns an array of dates between the two dates
function getDates (startDate, endDate) {
const dates = []
let currentDate = startDate
const addDays = function (days) {
const date = new Date(this.valueOf())
date.setDate(date.getDate() + days)
return date
}
while (currentDate <= endDate) {
dates.push(currentDate)
currentDate = addDays.call(currentDate, 1)
}
return dates
}
// Usage
const dates = getDates(new Date(2013, 10, 22), new Date(2013, 11, 25))
dates.forEach(function (date) {
console.log(date)
})
xxxxxxxxxx
<input id="first" value="10/26/2023"/>
<input id="second" value="10/30/2023"/>
Run code snippetHide results