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 trialAndrew Reynolds
Front End Web Development Techdegree Graduate 23,333 PointsQuestion in regards to saving a date to a variable
if (out) { const newDueDate = new Date(); newDueDate.setDate(newDueDate.getDate() + 14); this.dueDate = newDueDate; console.log(newDueDate) } else { this.dueDate = null; }
In the video we use the above code to save the newDueDate to the constructor property by the following: this.dueDate = newDueDate.
This then saves the date string to that property which can be accessed. I completely understand this. Where I am confused is that later on I tried doing the following:
const today = new Date(); const tomorrow = today.setDate(today.getDate + 1);
But when you log tomorrow it comes up with a long string of numbers : 1583970336437 I assume this is the milliseconds. My questions is whey when we save the newDueDate to the this.dueDate property is saves it as the following: Wed Mar 11 2020 16:45:36 GMT-0700 (Pacific Daylight Time).
I've tried reading the documentation and playing around with different methods, but I feel I can find the answer. If someone knows that would be great!.
1 Answer
Steven Parker
231,248 PointsAs you suspected, the "setDate" method returns the date/time in milliseconds which is simply a number. But the "dueDate" contains a Date object which will display as the readable date and time when converted into a string.
For more details, see the MDN pages on the Date object and the setDate() method.
Also, while not related to your question, your second snippet is missing a pair of parentheses:
const tomorrow = today.setDate(today.getDate + 1); // original
const tomorrow = today.setDate(today.getDate() + 1); // fixed
Andrew Reynolds
Front End Web Development Techdegree Graduate 23,333 PointsAndrew Reynolds
Front End Web Development Techdegree Graduate 23,333 PointsHi Steven, Thank you for the reply. I figured out my issues playing with the console but I do appreciate you taking the time to reply.
Above we are updating the newDueDate object using the " newDueDate.setDate(newDueDate.getDate() + 14); ". Then we save the updated object to the constructor property "this.dueDate = newDueDate".
Versus
Here I thought I was saving the updated today object to the "const tomorrow" where in fact I was only saving the returned milliseconds of the updated today.
Steven Parker
231,248 PointsSteven Parker
231,248 PointsThat's right, however the "today" variable does contain the new date at this point since "setDate" also updates the object directly.