const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>
(dateFinal - dateInitial) / (1000 * 3600 * 24);
// Example
getDaysDiffBetweenDates(new Date('2017-12-13'), new Date('2017-12-22')); // 9
best way to get the number of days between these two dates
from datetime import date
d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)
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
nbr_days = 365*year + year/4 - year/100 + year/400 + date + (153*month+8)/5
#Find the difference in days between two dates.
from datetime import date
start_date = date(2022, 1, 10)
end_date = date(2022, 11, 10)
(end_date - start_date).days
304
<?php
$date2='2022-08-1 2:10:00';
date_default_timezone_set('Asia/Karachi');
$date1 = date("Y-m-d H:i:s");
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
$posted_time_hours = round((abs($timestamp2 - $timestamp1))/(60*60)) ;
if(($posted_time_hours) >= 24){
$posted_time_hours = round($posted_time_hours/24) . " day(s) ago";
}
else{
$posted_time_hours = $posted_time_hours . " hour(s) ago";
}
echo $posted_time_hours;
?>
// new Date("dateString") is browser-dependent and discouraged, so we'll write
// a simple parse function for U.S. date format (which does no error checking)
function parseDate(str) {
var mdy = str.split('/');
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
function datediff(first, second) {
// Take the difference between the dates and divide by milliseconds per day.
// Round to nearest whole number to deal with DST.
return Math.round((second-first)/(1000*60*60*24));
}
alert(datediff(parseDate(first.value), parseDate(second.value)));