peeps of stackoverflow, so I'm working on this event for my site. I'm really close on figuring it out but I am stuck on an issue. The issue is, I get a NaN
returned when I'm trying to display how many days it has been since the last I updated a tab. Just side note, I'm manually updating the date via ID tag and JavaScript is doing the rest. Here is my HTML part of the event :
<span onload="manualDate('07-27-2021');" id="demoDate"></span>
Here is JS calculation for getting MS per day
const _MS_PER_DAY = 1000 * 60 * 60 * 24 * 30;
This part is stringing the date and modifying the numbers if they are single digits
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
Here, I am fetching the id value in the HTML span area and using that value and setting it
today = mm+'-'+dd+'-'+yyyy;
var newSetdate;
function manualDate(a) {
newSetdate = '"' + a + '"';
return newSetdate;
}
And lastly , This is the function that ties everything together. For some reason, the diffrence
valued is NaN
and im not sure why.
function dateDiffInDays(a, b) {
const utc1 = Date.UTC(a.getMonth(), a.getDate(), a.getFullYear());
const utc2 = Date.UTC(b.getMonth(), b.getDate(), b.getFullYear());
return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
const a = new Date('"' + newSetdate +'"'),
b = new Date('"' + today +'"'),
difference = dateDiffInDays(a, b);
document.getElementById("demoDate").innerHTML = difference + " days ago";