5.13 Date

The last embedded object we will look at in this section is the date object. Now, just to clarify, the objects we're looking at in this section are not the only built-in objects in JavaScript, there are others and you'll learn more about them as we go through the course. So let's look at different ways to create a date object. We can create date1 set it to a new date, so date is a constructor function. Now, when you open parentheses here, you can see this number here, 3 of 5, which shows different versions of this date constructor. You can use the up and down arrows to go through them, so we can use the date constructor without any parameters, which returns the current date and time. So let's rename this to today. Let's create another date object, new date, we have another constructor with 1 parameter called value, which can be a string or a number which is the number of milliseconds from January 1, 1970 or another date object. So here I'm going to pass a string let's say May 15, 2022, 9:00 a.m. This format is not the only format that you can pass here, if you want to know more about all the possible formats that are supported, just search for the JavaScript date, on this page, if you scroll down, here under the date string, you can read about the different supported formats. And finally, another way to create a date object is to pass numbers. So we have another constructor here, with these parameters, year, which is a number, month is also a number, and so on. So I'm going to pass 2022, now the confusing thing about isDate objects is that the month is based on 0. So 0 represents January and 11 represents December. So we'll use 4 for May. The third parameter is the date parameter, and that's the day of the month. So 15, the next parameter is the hour, so 9 is the hour and the minutes are 0. Now, we can exclude this argument, because all the other arguments are initialized to 0 by default. So that's how we create a date object. Now all these date objects have a bunch of get and set methods. For example, today. get , getDate returns the day of the month. We also have getFull year. getHours, milliseconds, minutes, etc. We also have the methods set So, let's call set FullYear and change the year to 2021. We also have setDate, hours, milliseconds, minutes, etc. Okay, now let's see how we can display this on the console. So save the changes, all these date objects have some methods to convert them to a string. You can call toDateString and we get a string like this. So Thursday March 18th 2021, we changed the year on line 6. We also have toTimesString which returns the time component of this date object. And another useful method is ISOString which returns a string like this. So we have the date, then T and the time, this is a standard ISO format, and it's commonly used in applications, so if you're creating a website or a mobile application that talks to a main server. This is the format that you will commonly use to transfer the date between the client and the server. That's it for this video on the date format in JavaScript we'll see you in a very next video.