Hey, everyone, in this video, we are going to talk about something super cool, which is date and
time in JavaScript, we can easily work with date and time using the date glass.
Don't worry about understanding what a class is right now, although if you want, you can just go to
Google and search for jazz classes.
Then you have a lot of things to read.
So you can understand better what classes are for now, just think of classes as an object, they are
actually more than this.
They are actually like a template for objects.
But for now, just think of the date as an object which will have properties and methods and everything
we've been working so far.
So to create an object with this class, we need to use the new keyword.
So that's the difference between a regular object and a class.
This time we need to use the new keyword, so let's create a new variable.
Actually, let's do an individual story called.
Let me just copy this.
So I'm just creating a variable.
And I'm using the new keyword, and then I'm just using date opening and closing parentheses, so this
means that I'm creating an object of the date class.
So now let's just do console that log so we can see what's inside this object.
So refreshing the page, as you can see, this is returning the date and time I'm recording this video.
Now, just out of curiosity, let's do type of.
And then Beit.
Object, so, as you can see, this is just a regular object.
But this is super cool because it just returned the current time and this is like the full version of
the date.
So as you can see, we have week, day, month, day, year, hours, minutes, seconds.
Then we have the time zone and then the information about daylight saving time.
So this is the complete date.
And if you want, you can also get specific information.
So let's say you just want to get the year what you can do.
It's just to date.
O'Bagy and then you can use a method called Get.
Full year.
So if you do this, then, as you can see, you are just going to get the ear back.
So in this table you can see some more examples so you can get to be extra careful with this, because
as you can see, the mouths, they start from zero to 11.
So it's not from one to 12 like we are used to work with mouths is the only thing you just need to pay
attention to.
So for the rest here, there's nothing new.
So for the hours we have it from zero to twenty three.
We have minutes.
Seconds.
Milliseconds, then there's something cool here that is the get time method's let's try to do this so
they Tobji instead of using the get foliar, let's use the get time method.
So as you can see, we are getting a big number.
So what is this?
This is the number of milliseconds since the epoch, which is January 1st, 1970.
So JavaScript uses the Unix epoch.
Every programming language has an apoc, which is used to set the starting point of counting time,
so this is useful to do calculations with date and time.
So like I said, the Unix epoch, which is used by JavaScript, is January 1st, nineteen seventy zero
zero zero UTC time.
So this does not mean that we can't work with dates prior to 1970.
We can do it.
We're just going to have negative numbers.
So anything after nineteen seventy, then we will have this positive number of milliseconds, so this
is the number of milliseconds that have passed since the book.
So now why is this useful?
Why do we need a starting point to have this number of milliseconds?
Because when we work with this, which is actually called a time stamp, so every time we work with
milliseconds since the APOC, we can call it time stamp.
And when we are working with time stamps, we have more flexibility to easily do calculations with dates.
So if we want to get how many years have passed since the apoc, this is pretty simple to do because
we have the number of milliseconds, then we just need to know how many milliseconds we have in one
ear.
So here.
I have this table, so I know that one year has this number of milliseconds, so if I get all this number.
And I just divided by the number of milliseconds in one year.
We can see that this is something close to 50, which is accurate because I'm recording this video in
2020, so from 1970 to 2020, we have 50 years.
This has that because I'm in the middle of the year.
So if I just want to get the number of years I could do like this and then just use the math.
That floor, which is going to round number, is down.
So now I know that 50 years have passed since the epic.
And I know that maybe it's not very useful to get how many years since the apoc, but this is going
to be useful to compare to different dates.
So let's do a few tests with this.
But first, let me teach you how to create specific dates, because so far we just created a new date
object and we did not pass any argument.
And when we do this, it just creates an object with the current date and time.
But we can also use something specific.
So if you want to create a specific date, you can pass from one to six argument.
So let me just clear this real quick and now.
Let's do a few tests, so I'm just going to do a new date again, and if we do this, we can see that
it's creating this object with the current date and time.
But let's try to use one numeric argument.
So if we use just one, it's the millisecond since the apoc.
So let's just get the number of milliseconds in one year just so we can see how this works.
So if we do this, we can see that we created a date in 1971, which is exactly one year after the APOC,
so here we can also use a different number of arguments.
So if you use two numeric arguments, it is just going to consider it to be year and month.
So let's try again with two arguments.
So the year let's try, I don't know, 2010 and then the month.
Don't forget that the month they go from zero to 11.
So if I do this, as you can see, January 2010.
Then we can do the same thing with three arguments, four or five up to six, you can read.
All the details here.
We can also bass strings as arguments in three different formats.
So if we create this object and we pass the ISO date.
Which is formed by the full year, so 2010, let's say, then two digits for the month, then it's like
we know it's like January, it's just zero one.
And then the day, let's say 18.
As you can see, we just created a specific date.
We can also use the short date, which is written like this or month day in here.
And we can use the long date as well.
Which is going to work like this, so April is going to be a PR April 10th.
Two thousand and twenty two.
So here you go.
We just created this object, and now that we have this object, we can get any information we want
from it so we can get the number of milliseconds since the Abuk, which is useful to do calculations,
we can get specific information like just the month or the hours or minutes.
So how can we use all this in a useful way?
So there will be two very nice examples and one of them is going to be the challenge for this lesson.
I want you to calculate date intervals.
So let's say we have a product that was ordered on March 26, 2020, and it was delivered on April 2nd
to 2020.
I want you to use date object to calculate how many days does it take this product to be delivered.
So just calculate this using date objects and show the number of days here.
Just take your time, try to solve this.
And in the next video, I'm going to fix this.
And in the next lesson, we are going to talk about time methods in JavaScript and there will be another
very cool challenge for you to do.
So I'll see you in the next video.