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 trial

JavaScript Practice Object Interaction Checking Out and Returning a Book Demo: Charging Fines to Patrons

anmo20
anmo20
6,470 Points

const dateDiff = new Date(now - patron.currentBook.dueDate); is producing a date in 1970

I copy and pasted the solution into my program, but noticed some weird behaviour. Despite that, it still seems to run correctly. What's going on?

For reference, I set the due date to be 2 days in the past for testing purposes. In the Book setter function from earlier. newDueDate.setDate(newDueDate.getDate() - 2);

This statement: const dateDiff = new Date(now - patron.currentBook.dueDate);

This is giving me a value for "dateDiff" of: Fri Jan 02 1970 17:00:02 GMT-0700 (Mountain Standard Time)

Yet the following daysLate variable is still coming back with "2" and the correct charge of 0.2 is being applied to the patron's balance.

What the heck is going on here?

1 Answer

Simon Coates
Simon Coates
8,481 Points

I haven't done the video, but I'm going to guess what happened and run a demo.

var x = new Date();
// a couple second later, I ran
var y = new Date();
x - y;
//ouputs -8592
new Date(x-y);
//outputs Thu Jan 01 1970 09:59:51 GMT+1000 (in my timezone)

I think dates are number of milliseconds since the epoch (which i think is 1970). So the difference between two dates (relatively close together) in milliseconds will produce a date in around 1970.

anmo20
anmo20
6,470 Points

Hmm that's interesting. Quite odd!