Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMuhammad Umar
7,817 PointsUsing Console.log on const dateDiff gives the date Wed Jan 14 1970 18:00:00 GMT-0600 (Central Standard Time) ??
Hi there,
So performing console.log
on const dateDiff
in the provided solution for Charges Fines to Patrons, gives the date output Wed Jan 14 1970 18:00:00 GMT-0600 (Central Standard Time)
. Can someone please explain why is it showing date from the 1970? When I console.log
the const daysLate
it shows the correct number of days, 14. I am hoping someone can explain how it is providing the answer 14 by taking today's date and subtracting the dueDate which seems to be from the 1970's.
for (let patron of latePatrons) {
const dateDiff = new Date(now - patron.currentBook.dueDate);
console.log(dateDiff);
const daysLate = dateDiff.getDate();
patron.balance += this.dailyFine * daysLate;
}
console.log(dateDiff)
Wed Jan 14 1970 18:00:00 GMT-0600 (Central Standard Time)
I also tried the following code below but that does not seem to be the correct approach as it takes today's date->day (14) and subtracts it from the dueDate (14 days ago, which makes it April 30th). This outputs -16 as the total number of days instead of -14, which is incorrect.
for (let patron of latePatrons) {
const dateDiff = Math.abs(now.getDate() - patron.currentBook.dueDate.getDate());
console.log(dateDiff, now, patron.currentBook.dueDate);
}
What would be the approach where the correct dates are shown and the difference provides the correct # of days, 14?
now = Date 2019-05-14T13:06:27.319Z
patron.currentBook.dueDate = Date 2019-04-30T13:06:27.318Z
Any help is be much appreciated.
2 Answers
Steven Parker
231,248 PointsThe difference between two dates is not a date itself but a span of time. In this example, that difference is 14 days.
But if you convert it into a date, it will be considered as the date that is 14 days after "date 0" which is also known as the "epoch". And the epoch for timestamps is Jan 1, 1970.
But you don't need to treat it as a date and call getDate, plus that will only work for spans of 31 days or less. Instead, you can just divide it by the number of milliseconds in a day and round down:
const msLate = now - patron.currentBook.dueDate; // date differences are milliseconds
const daysLate = Math.floor(msLate / 86400000); // convert to days
Muhammad Umar
7,817 PointsSteven Parker, thanks so much for taking the time to answer. As always, really appreciate your help. This cleared up my confusion.
Brendan Moran
14,052 PointsBrendan Moran
14,052 PointsThis is what I did in my solution. I really don't like using a new Date instance to find the difference because of the 31 day limit. Makes a lot more sense to just work with the ms and convert to days.