View Full Version : Resolved Date Difference Computation

09-18-2019, 01:55 PM

The code below appears to be not working. Might someone be able to point out the error of my ways?

var now = new Date();
var awoke = setFullYear(2019, 8, 18); // September 18, 2019
var difference = (now - awoke); // raw time
var daze = Math.floor (difference / (1000 * 60 * 60 * 24)); // convert to days
document.write (daze); // the envelope, please

Thank you!


09-18-2019, 02:45 PM
Try running it in a browser and you get the error

Uncaught ReferenceError: setFullYear is not defined
Take a quick look at the definition of setFullYear (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear)
Its a method on the date object.

var awoke = now.setFullYear(2019, 8, 18);

So if you change that, the code runs, but won't work as intended, as it modifies the date object it is run on.

If your intention is to get the difference between today and a specific date, try the following:

let oldDate = new Date("08/18/2019");
let currentDate = new Date();

let miliDifference = currentDate - oldDate;
let daysDifference = Math.floor(miliDifference / 86400000);

09-18-2019, 03:28 PM
Thank you.

I'll add an "official" thank you when I figure out how. (It's been a long time.)