Charge your phone, we're going on a date.
In the last few lectures we worked with prebuilt functions and objects. In this lecture we're going to actually create an object. The first object we are going to look at is the Date object. The Date object enables us to play around with the date and time. In the process we are going to learn how to create, instantiate and play with objects.
Video Transcript
00:02It's time for me to recharge my phone and it's time for you to create your first date object
00:08How route how that's so karmic as if we're doing the same thing at the same time. So
00:13That's really the first object out of many objects that we're gonna be creating until now
00:18We saw objects that there's always existed like the math objects
00:22There's no reason for us to have more than one of them. So they're always there
00:26Same as with our document. We only have one document. So why would we ever need to create a new document?
00:32But contrary to that, for example the date object, although there's this current date
00:36But what if we wanted to create the date of something that was 20 years ago or in a week from now?
00:41So we want to be able to create our own
00:45Date objects and do the that we're gonna use the term new or we're gonna learn about this new key term. That's called new. So let's
00:53Do it
00:54One object and by the way also the console is an object an object that helps us
01:02Interact with the console that helps us log in a few other things
01:06We've seen also the math how we interact with it that math object and we've seen how do we interact with the document object?
01:13The next thing I want to talk about is actually creating your own object
01:17Which is the first time we'll ever do it and what I want to do is I want to output the date today's date
01:23Now to do that, I'm gonna create a new variable. I'm gonna call it date and I'll set its value to be a new
01:30Date now this
01:32This is a very weird context
01:48though a very weird might look really weird because this looks like a function and this is the word new and
01:55Literally, that's the process of creating a new object
01:58You basically say the word new and then you define what type of object and there's a lot of pre-built objects
02:03Now you would never make a new math because that doesn't make any sense and it's not permitted
02:09The math is not saying that you instantiate. You never would really create a new document either
02:14But on the other hand, you would want to have maybe a few different dates now in this case
02:19Once we create the date object, I could go ahead and even directly just console
02:25Log and log out my date
02:28Now there you don't have to understand the the the process behind why it works
02:33You just need to know that it works when you say the word new you put a space and then you put the name
02:38Of the object and there's a lot of objects
02:40Obviously, you can't just put you whatever you want
02:42It has to be one of the stuff that exists such as the date object
02:46And if I save that and go and run it and look at what is gonna output inside of my my date object
02:53We'll see that I'm gonna get here the current date as an object and in that object
02:58We'll see that today is what when I'm making this recording. It's October 30th
03:032014 but beyond that I could go to that date object and I could try to get the date for example
03:11Oops
03:12So the date oh by the way, this is a great example because the date doesn't exist
03:19Why does not it does a little why does it not exist?
03:23It doesn't exist because if we look at here
03:26We'll see that all this is created inside of this function and when I'm trying to call out
03:32I'm actually calling it when it's not inside of a function
03:36We're out of the function already so we can't see anything for us to be able to see it
03:40We'd have to write new date and then approach
03:44So for example get day as that function and create a completely separate object
03:48Because we don't have access to it once that on ready was complete
03:52And when we were trying to look at in our console this variable doesn't exist
03:56Another way around is if I would take this variable and save it outside of that function
04:01So it's publicly available outside of that function as well
04:05And then instead of creating the variable again
04:07I would just assign the value into it and in this case if I click on refresh
04:37Then I'll have access to that date object and I would be able to approach for example that get date again
04:43And just see what value would come back so in this case the date itself is the 30th or I could approach to get
04:50Get day, and if I approach to get day
04:52I would get the day of the week that we're in from zero to
04:57Six so in our case we're on a Thursday, so that is number four
05:01So let's take a deeper look so besides creating a new date the date comes and you might think well
05:07Why do you need to create a new date? Can't we just ask for the date?
05:10So technically we could there's a function called and console dot log
05:15there's a function in JavaScript called date uppercase D and
05:19If we just run that function inside of our log, we'll see that we could get the actual current date so really the power of
05:26Working with an object doesn't really come to fruition just from the date itself
05:31Really the power comes when we want to create a custom date, and I want to show you examples of that
05:36So this is our date without any parameters, which would give us our current date
05:42We could create also the date
05:45But by setting inside a value inside of that new date and sending a parameter any parameter
05:51It could be zero and once we set the parameter
05:54It's going to add whatever value put here in milliseconds to the date the 31st to
06:02December 1969 I have no idea why it's something based on more or less
06:07That's when the computers were created
06:09But it doesn't really matter and you can see that we're getting in our object now that it's 1969
06:14but if I send inside of here a value, let's say of
06:181000 milliseconds and we refresh again. We'll see that the time now will be
06:24Basically, it's midnight in you know, like GMT so like your GMT minus five
06:29So because I'm in New York, so you can see that it one second passed from that date
06:33So we could literally define the date and beyond defining the date we could very easily and very creatively
06:39Go ahead and create some sort of a clock or some sort of a date that shows us the time and all that stuff
06:45Which will how about we'll create that in this literally in this chapter, but before we do that
06:52I want to show you another way of approaching our date object and another way of creating and approaching our date object is literally
06:59Going to our date
07:00Again, I'm gonna create it and create a new date
07:05And the first property I'll send as the year
07:08The second property will be the month and the last property for now will be the day and there's more
07:15Properties such as hours and minutes and seconds and so on and so forth
07:18But we're just gonna fill up the first third once we fill out more than one property one parameter
07:24It will automatically address it as if we're putting inside actually the date itself
07:29So in this case, I'm feeding in the date the 19th of October 1982
07:33Because I want to know what what day was I born and I could see now that I was born on a Friday
07:39Oh and I put your it's November my bad
07:42So let's go and fix that and that is because the value here starts from zero
07:46So if I wanted the value to be the 10th and wanted to be October
07:50I would put there 9 because 0 1 2 3 4 5 6 7 8 9
07:53So my bad by the way, the 19 is good. The year is good
07:57The only only the month for some reason is not in that in that order. It starts from zero instead of starting from
08:05One and you can see that now we're in October 19. They can see that I was born on a Tuesday, which is awesome
08:11So if you don't know what day you were born
08:13You could very easily just go into your computer and type it into these three parameters
08:17The year the month making sure that you subtract one from that month
08:21So it's from 0 to 11 and the date which you'll just put it as as it is and then you would know the day
08:27You were born. How about that?
08:28So you thought you're coming to learn how to animate and you're learning what day you were born in if you don't know what day
08:34You were born in so that's it for that. That's that's it for learning about the built-in
08:40objects and how to approach them
08:43how to get from them stuff how to
08:46Talk to elements that are on are on our stage
08:49And how to create a completely new object and we sampled it with our date object now
08:56We're far from being over with talking about objects
08:59We have a lot more to talk about objects in this chapter what I'm going to what we're gonna do in the next
09:05Lesson is we're gonna revisit our
09:08tween light tween max now that we know it is an object and we're gonna revisit our
09:14jQuery now that we know that it creates objects and we're gonna
09:18Update our code here to make sure that it's gonna be that it will work on different browsers
09:24And we're also just take advantage of this fact and create maybe a small
09:28Animation of some sort that is not gonna actually use the library but an animation that will use our
09:34capability of showing the time so our goal in the next lesson will be to maybe change the headline here to be actually the
09:41Date and time and have it update every certain amount of time
09:46All right, so see you in the next lesson when we approach that
