Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Notice that, it's not just the date; it's the time. It's actually the time down to the millisecond. Now, you don't always want a date object that exists for today. So you could create another one which is a new Date and then passing in year, month, and day. But notice month is 0 based like an array; it goes from 0 to 11. The year is the real year, the day is the real day 1 through 31, but month is 0 based here. You can also get it down to, in this case, seconds, year, month, day, hours, minutes, seconds if you want to be very specific.
Internally, a date object is stored as the number of milliseconds since the 1st of January 1970, which is a pretty common way of doing it in the programming world. Once you have a date object, like having an array or like having a string, you can do things with it. We have a lot of methods of the date object that begin with the word Get. So after creating a new object, I can say today.getMonth and it returns 0 through 11. I can say today.getFullYear to give me the four-digit version.
That one is not 0 based, so it would return 2011, 2012, and so on. There is actually a today.getYear, but that one is officially deprecated, meaning that it would work, but you're not supposed to use it anymore. The recommended one is getFullYear. We have today.getDate. Now, for those of you coming from things like SQL, that might sound like something that should return the full date, but no. Here that will actually return you the day of the month, and it's 1 through 31, as opposed to today.getDay, which would return 0 through 6 for the day of the week and 0 is the Sunday.
An example of using this is if I define say a new Date, and here I'm doing a date that is 1906, December because we're 0 based for the month, and the 9th. That's actually Grace Hopper's birthday or Grandma COBOL. So I'm going to then write out console.log, give it a string, then a comma, and then I will call myDate.getDay(), opening and closing parentheses just to say I'm passing no parameters there.
I then close the parentheses of the console.log call and finish the statement, rerun that. Apparently, Grace Hopper was born on a Sunday, and simple silly example there, but that's how you start to get into the deeper parts of the Date object. We even have today.getHours. This one is also 0 based, 0 through 23, and there's today.getTime. Now, this one will actually return the milliseconds since the 1st of January 1970. Now, that sounds like it's a little bit esoteric, but in fact getTime is one of the most useful methods of the Date object, and you'll probably see it used quite a lot, because it's a very direct way to figure out, is something less than something else? If one date is less than other one, I don't have to bother comparing years and months and days, if I can just get the big number that represents the milliseconds.
It's realizing there are two different objects and saying this is false. We need a simpler way to compare them, the way that we can compare say numbers, which is very easy. So what I'll do instead is I'll ask if date1.getTime-- that's that really useful method that just returns milliseconds--is equal to date2.getTime. Now, calling both of these are just going to return milliseconds. It would be like comparing two numbers, and this would return true. Now you can also use this very easily with comparisons such as greater than, less than, greater than or equal to, less than or equal to.
So this is one of those areas where the methods of the Date object come in very handy indeed if you don't want to have to manually start comparing year parts and month parts and day parts.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.