And welcome to JavaScript Dete Lesson, we're going to take a closer look at what's available for date.
Now, there's a lot of different ways to use JavaScript date, so let's take a closer look at some of
the options we have available for date.
And there's several ways to create the date object where you can set that current date.
So if you just do a new date getting the date object, that's going to get the current date.
You can also set the date object to a particular date.
There's a number of different ways that you can set the date object so you can divide it up within the
year, the month, the date, the hour, the minute, the second.
So you can set it that way.
Or as well, you can write in a number of different ways.
And this is all going to create that same date object, although the third one here is going to create
it at a default time because we're not specifying the time of that date object.
So let's see what else we can do with date.
And there are a lot of things you can do with date.
So the way that date works is that JavaScript creates a date instance that represents a single moment
in time.
We can take a closer look at that within the Bobzilla developer network at the docks where we've got
several different examples of date.
So date is written and this is actually the same date.
So there's more information as to how to use date, of course, down below.
And there are a lot of things, including looking at the time stamp, which is a representation of a
number milliseconds since January 1st, 1970.
And we can calculate out how many seconds since that time as well.
You can get the year, you can get the month.
And this is a zero based.
So starting at zero.
So January is zero.
December is 11th.
So keep that in mind when you are creating the dates that when you are using month index that this is
zero based.
There's also the day.
So this is an integer representing the day, hours, minutes, seconds, and you can even go to milliseconds.
And if you are getting the milliseconds once again, it is based on that January 1st, 1970, and the
date object range is one hundred million.
So negative hundred million to plus one hundred million.
And the good thing about the data, the date object is it provides a uniform behavior across platforms.
So if we use date now, it returns a numeric value corresponding to the current time, minus the number
of milliseconds that have elapsed since January 1st, 1970.
And there's also more that you can do within the date prototype where you can get the dates as well
as the day, the full year, the hours, the milliseconds from the date object.
Once you can get something, you can also set it.
So setting the date, setting the full year set in the hours.
So this gives you the ability to manipulate the date object.
So we try it within the editor.
So first of all, let's get our new date.
So just using the due date of method in order to set that date, and I'm going to output it into the
console so we can see what is contained within date.
So it provides a string value of the current date.
There's also using the date object and we can get now.
So that returns back again.
Those millisecond we can create a new date as well so we can take D so creating a new date object and
we can set the date.
And then we saw there's a number of different ways to set it.
So if we want to set it to twenty twenty and this is November, so keeping in mind zero based for the
month, we can set hours, we can set minutes, we can set seconds.
We can even set milliseconds if we want.
So we've got Sunday, November 15th.
Twenty twenty.
So that's the year, the month, the day and so on.
So that's the hours, the minutes, the seconds and the milliseconds and notice again.
So if we go to 11, that sets up to December.
If we do 12, then what happens is we end up with January.
So January is 12 and it's also zero, but we're adding to the year.
So we're just incrementing the date.
So if you had a particular value, if you wanted to add to that date value, so if you had, let's say,
for instance, you had 11 and you want to return back and increase it by one, you can always add one
to the value and that will increment and change the current date object that's being output.
New date.
You can also use the milliseconds as well.
So setting date.
And if you set it to zero, you're going to see that you're going to get that December 31st, nineteen
sixty nine at seven p.m. because this is GTM time.
So using that same date starting January 1st, 1970, and we can count up from their number of milliseconds,
we get 2001.
And if we had another zero and this is again milliseconds from that January 1st, 1970.
So now we're at twenty twenty six to eight as the year that we can apply string methods to it and outputting
it as a string so it returns a date portion of a date object and human readable English.
So Wednesday, April 3rd, 2019, to you see it converted and this is more use than the UTC story.
And as we saw, there's a number of different ways to set the date.
So if you are to take the dates and create a new date object from various formats, and this is really
super useful because of the fact that we can utilize it from different input formats.
So we see that this one, we can set the month as well.
And it comes back December because we're not using the zero based we're using a string value in order
to set the date.
And we can use the dashes, we can use the slashes.
And there's a number of other formats that can be used as a standard in order to output and create the
dates.
You can also get values from the dates.
So if we do D equals so the get date gets the date.
So this is returning back the date and it's going to move a few things around here.
So I'm going to set up the vowel as it's probably better to set Val and then also using D.
So it's getting the fourth day of the week.
Date is getting the date, so the numeric value and then get date is getting the day of the week.
So this is a value from zero to second.
And if we update it to 27, then we see it starts at zero.
We can also get the time as well.
So that's another option.
So as mentioned, there's a ton of different options here.
So the time once again is returning back that numeric timestamp.
So it's milliseconds again, can do value add D get milliseconds so you can get the full year, you
can get ours, you can get millisecond.
And because we didn't define any, it's set to zero because this one is zero where we're setting the
milliseconds.
Let me comment all of those out.
We see that now.
We can see that we've got 34 the milliseconds and now we've got 40 for the millisecond.
We also get minutes, get the month, get the seconds, get the time.
And there's also one called Time Zone Offset.
So it returns that time zone offset from UTC in minutes for the current local time.
So that's another useful one.
Depending on where you're getting these date objects, you might want to use the local time or the UTC
time.
So if you get milliseconds in UTC, it's actually going to be the same because that's not being affected.
The one that's actually being affected is the hours if we get ours and then UTC hours, you're going
to see that there is a difference between get hours and UTC hours.
So UTC is ten because that's getting it, according it to universal time.
And then this one is local time.
So what if we wanted to get the time of tomorrow so that tomorrow's date.
So let's set a parameter and we'll make it dynamic.
So set this one and we can adjust this.
And then using CONSED, we're going to get a new date and equal that to new date object and we need
to set the date.
So we want to get tomorrow's date.
So taking data using now so that this returns back milliseconds, we're going to add it and what we're
adding is days, but we need to multiply it to get the right milliseconds.
So there's 24 hours in a day.
There's 60 minutes in an hour, 60 seconds in a minute, and 1000 milliseconds in every second.
So now we can get the new date is going to contain tomorrow's data.
So you can see it being output there that April 4th.
And today is April 3rd.
You can try this one and we can adjust this as needed.
So if I set it to zero days, then it's going to return back today's day.
If I do ten days, then it's going to go to ten days from now.
And if I do 100 days, we're going to see it's going to do 100 days as well.
So this is a more accurate way of setting the date instead of incrementing each day, because as we
saw, whatever the increment, the days, sometimes things get thrown off.
So you don't always have that.
Each week is going to be seven because you might be at the end of the month or at the beginning of the
month.
So things might change.
And there's an example within the guide for this, how to add a day.
And then also what you can do is you can try to add a week as well using the same format.
So there is a challenge for this lesson.
And the challenge is to get the number of milliseconds since January 1st, 1970 to your birthday and
then output that into the console.
And I've got an example of the code so you can pause the video.
And, of course, you're going to get different birthday than me unless you're born at the exact same
time and then get the milliseconds into the console.
So go ahead and try this out.
And the code is just down here below.