const today = new Date();
var date2 = new Date("10/29/2021");
var Difference_In_Time = date2.getTime() - today.getTime();
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
document.write(
"Total number of days between dates <br>"
today
"<br> and <br>"
date2
" is: <br> "
Difference_In_Days
);
CodePudding user response:
Try this
const date1 = new Date('9/20/2021');
const date2 = new Date();
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffTime " milliseconds");
console.log(diffDays " days");
CodePudding user response:
there is two problem in your code
- you are substracting old data with current which will give you negetive value since current is always bigger because time is going forward always
- there is problem in your formaula to divide miliseconds into day i have corrrected this below
const today = new Date();
var date2 = new Date("10/29/2021");
var Difference_In_Time = today.getTime() - date2.getTime();
var Difference_In_Days = ((((Difference_In_Time / 1000) / 1000) / 60) / 24);
console.log(
"Total number of days between dates <br>"
today
"<br> and <br> \n"
date2
" is: <br> "
Difference_In_Days
)