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 trialnfs
35,526 PointsI don't understand the part when day is assigned to new Date().getDay();
How does this part work in the video? Thank you for your time.
3 Answers
Jennifer Nordell
Treehouse TeacherHi there! I'm not sure how far you've made it into Object Oriented programming, but the Date
object is one supplied by JavaScript. In this line we are creating a new instance of the Date object and calling its getDay()
method.
var day = new Date().getDay();
The Date
object supplied by JavaScript represents the local time where you are. The getDay()
method returns a number that corresponds to a day of the week starting with 0 for Sunday. Here's the MDN documentation.
Hope this clarifies things!
nfs
35,526 PointsOh, thank you. Appreciate it. But you see, I understand that but in the video,
var day = 1;
if ( day === 0 ){
//output
}else if( day === 1){
//output
}else if( day === 2 ){
//output
}else if( condition ){
//output
}and so on...
Now my question is why only changing the variable and assigning it to
var day = new Date().getDay();
How can the whole thing still work without changing the conditions? I do think that this whole thing:
new Date().getDay();
gives me a number between 0 and 7. Am I not right?
And thank you once again.
Jennifer Nordell
Treehouse TeacherHi there! Again, look at the documentation. It does return a number which corresponds to a day of the week.
The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday. For the day of the month see getDate().
Return value An integer number corresponding to the day of the week for the given date, according to local time: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
Hope this helps!
I updated my original answer as what I posted wasn't entirely accurate. I meant that it returns a number which we then use to associate with a day.
nfs
35,526 PointsOh yes... Thank you very much.