[MUSIC]
>> Hi there. I'm Christopher Harrison,
Senior Program Manager at Microsoft Cloud AI.
Let's take a look at how dates work inside of JavaScript.
Now, I don't need to or mean to necessarily get like
all deep and philosophical but time is just a construct.
Or at the very least in JavaScript terms,
time is an object.
So let's take a look at this object and see
how we can utilize it to manipulate time.
Well, at least in our code,
not necessarily in the real world yet.
So all my bad jokes aside,
working with dates and times in JavaScript,
centers around one object and that is the date object.
This date object is going to actually
contain both the date and the time.
Now one thing that I want to highlight here is
the fact that internally time is actually
stored as the number of milliseconds since January first, in 1970.
This will actually become a little bit of a thing later on.
But I just always like to call that out right up front.
Now, to create a brand new date object,
we simply say const now equals new date.
That will give us the date and time for right now.
Well, what happens if I need to program
something where I'm going to be working with a particular date?
I need to make sure that I'm able to go ahead and set that.
Well, there's actually a few different constructors
that are available to us with date.
One of them will give us the ability to specify the year,
the month, the day.
This would be 2015,
the 12th day and then well actually the fourth month.
That one big thing to keep in mind about the way that months are
specified on the object is they start with zero.
So zero would be January,
one would be February, two March,
and then three would actually be April.
So this would be April 12th, 2015,
and then the time would be a 06:00 AM,
24 hour clock, 25 minutes and 58 seconds.
If all we want to do is specify the year,
the month, and the day,
then we can do that,
and this would be 1995, August,
because again, remember starts with zero and then the 24th.
Now one thing to keep in mind is that when we
print out the day is by
default and you can always go in and
specify different formatting and so forth.
But by default, it's going to emit it
using a standardized output and you
are going to notice that it will specify
April and August more traditionally there.
So internally it's doing it as zero.
But when it goes to display it out as part of a string,
it is going to display it
"correctly" or at least the way that we as humans would expect it.
While I might personally want all counting to start with zero,
I'm probably relatively unique in that aspect.
As far as setting all the individual values go,
you're going to notice that there's a whole set of
different set methods that you can use for the year,
the month, the date,
the hours, the minutes, and the seconds.
Now the three that I want to call out here real quick.
First is month.
Again, remember, counting is going to start at zero,
the date that's actually going to set the day
and then again that hours is going to be the 24 hour clock.
So by the time that we're all done here in running this code,
it's now going to be April 4th of 2014
at four in the morning and then 24, 46.
Now you might notice the display there of 11,
and the reason for that is because it's doing
an offset of UTC on the display.
That's why it's showing us 11 down below there.
But at least for my time,
which is currently Pacific time,
it would wind up being four in the morning there.
To get our values,
you're going to notice that just like all the set,
we have a whole bunch of get as well.
So get month will get us the month,
get day will get us the day.
Sunday, Monday, Tuesday, where Sunday starts at zero and
then last but not least, is get time.
Now one big thing to keep in mind about get
time is the fact that it's going to give us
back the number of milliseconds since January first of 1970.
So that could be the potential output if we
add it in just a little bit of string formatting.
Speaking of adding in string formatting and actually running
our code and doing all of that, let's go do it.
Let's take a look at how we can
work with dates inside of JavaScript.