02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

It's your birth date

Objects, they're not new to us anymore. As we discovered in the last few chapters, functions, numbers, strings and Boolean Data types are at the heart of JavaScript and Objects are an extension of these basic building blocks. In this lecture we continue to explore and manipulate the Date object. We will use the Date object to calculate the seconds since our birth.

Video Transcript

00:00happy birthday well that is if it is your birthday what I wanted to talk
00:04about now was about my birthday and we're not talking about years although I
00:09just turned 32 a little bit ago I'm actually celebrated my 1 billionth
00:15second on earth recently which I thought was way more exciting than just 32 oh
00:22I'm going in a tangent we're talking about programming so do you want to know
00:26how many seconds have you been on earth already I hope you do because that's
00:29what we're going to discover it in this lesson and beyond that we're going to
00:33practice more the relationship between objects and we're going to start
00:37opening things on the stage getting ready to create a clock a time clock our time
00:43alright but we're going to start in this lesson and throughout the next few
00:48lessons by the end of them by the end of this chapter we're going to have a
00:51really wicked rad clock that is not going to be the actual time of the day
00:56which you'll be able to do as well but it's going to be actually time that
00:59passed since you were born our goal is to output here maybe the amount of
01:09seconds that passed since my birthday my original original birthday so let's go
01:15ahead and start doing that but the first thing that I want to approach is I want
01:18to make sure that I'm not really referring to my h2 this way and as we
01:23said because I want to make sure that this application works also for people
01:26that are not running in HTML 5 so what I'm gonna do is I'm just gonna
01:31comment out the section and instead of that I'm gonna create here a new
01:35variable right below it I'll call it my h2 as well but instead of approaching my
01:42element by using the get element by ID I'm gonna use the jQuery don't forget we
01:49already read we loaded jQuery so we could use the jQuery function of dollar
01:53and all we have to do is send inside here a string with the hashtag and then
01:58the ID in this case my ID is my h2 so I'll just send here my h2 and there we
02:04go and now I have my h2 with a lot of extra functions that are baked in with
02:09our jQuery so I could literally now go ahead to my h2 and start changing things
02:15such as changing the text with the text function and changing it for example to
02:19north and if I literally just run my application now once I've done that
02:23I'm just gonna go ahead there save it click on refresh you'll see that now
02:27it's updating to north now by the way literally what we're doing here is we're
02:32approaching and creating a lot more accessing the object of h2 just like we
02:39did when we got the element by ID but we're wrapping it with a few other
02:43functions that help us do things such as the text function because in their HTML as
02:48well is not wasn't the standard ten years ago five years ago but if I'm
02:53using jQuery jQuery will make sure that as long as I'm working with the
02:57jQuery that is compatible when you download jQuery it tells you to what
03:01browsers it's compatible so in this case because we're using jQuery 1.11 there's
03:06already jQuery 12 jQuery 2 jQuery 2 already doesn't support at least
03:12currently IE6 but because we're working with 1.11 then we're making sure that we
03:16have the support of IE6 as well but if you don't care about IE6 you could
03:20always upgrade to jQuery 2 now with that said I could approach all this in this
03:28way or I could write it in a shorthand way without even creating a variable
03:32name I could literally like we did before where we approached the object
03:36once the object gets to me I could or put it into a variable or I could just
03:41approach it directly by typing dot and then doing whatever I want to do to it
03:45in this case I want to call the text function and send into it new text I'm
03:49just gonna put here in NARF2 so notice how quick and shorthand that is and once
03:54you get used to it how intuitive it is because literally this replaces these two
03:58lines it replaces these two lines and it's short and it's easy now the only
04:03disadvantage is if you want to approach this a few times and in that case you're
04:08probably better off creating a variable because every time you call a
04:12function you're adding a little bit more processor power on your operating
04:18system but in our case because we're only calling it once here it doesn't
04:41really matter so we could just approach it in this way all right so let's
04:44continue and really like we were saying earlier my goal is to create basically a
04:50way to calculate how much time passed since my birthday now I want to make
04:56things a little bit more complicated because I want to integrate inside our
05:00core our chapter about functions so I'm gonna go ahead and create here a new
05:04function and in this function what I'm gonna do is I'm gonna call this how get
05:11second seconds from birthday that's gonna be the name of our function it's
05:17gonna accept one parameter which will be the year one parameter that's gonna be
05:22the month and one parameter that will be the day or date all right so we have
05:29your our year month and date and what we really want to do is we want to
05:32return back the amount of seconds that passed since our birthday so I'm gonna go
05:37ahead here and I'm just good for the sake of our sample I'm just gonna just
05:42undo the the console logs we don't see them and I'm just gonna go ahead and
05:46just call the function get seconds from birthday and I'm gonna call in using
05:55the parameters of my birthday which is 1982 which is October which is 19th of
06:00October and we're gonna make sure that we fix it in and I'm just gonna save
06:05this as seconds and I'm gonna go ahead once I saved it and I am all that's left
06:13for me to do is actually to fill in the logic of this function and it let's start
06:17doing it so first of all we're calling the function the values are coming in I
06:20want to create here a new date object so I'm gonna go ahead and create a new
06:24date object set it as a new date now I want you to know a few things I'm
06:30calling the variable named date even though there's a date object that was
06:33already created that was called date I could call this whatever I want now the
06:38most important thing is that you must realize is that if I create the name that
06:42is the exact same name as something that is above it then I'm not gonna have
06:46access to this object so when I'm in here if I want to refer to that date
06:51object out of here I would have a really rough time because whenever I use the
06:55name date and the line below it's gonna refer to that internal variable
06:59called date so if you need to have access to a variable outside you'd want
07:05to have your a completely different name in my case because I know I'm not going
07:09to be trying to access that variable from within this function then I'm fine
07:13with putting here the same name now I know this might become a little bit
07:16confusing and by the way this is exactly where a lot of developers kind of have
07:21a lot of issues so you have to get used to deliberately using the same
07:25name you want to make sure that you realize the difference between this
07:29variable and the variable above now I want you to even comment in the comment
07:34box below and explain to me from what you understand so far what is the difference
07:39between this variable that we're defining inside of this function versus
07:42the variable that we're defining externally here and do we have if I was
07:47out here do I have a way to refer to this variable so I'm not gonna answer
07:51this question anymore unless you go into the comment section and tell me
07:56what you think and we'll talk about it in the comment section so let me
08:00continue so I want to send in these properties that we're sending through that
08:031982 October 19th so I'm going to send in that first property of year the second
08:09property of month and as we said earlier we want to make sure that we
08:12actually subtract one from the value because we want to make sure that our
08:16date object is getting a value values between zero and eleven and last but
08:20not least we want to pass in our date as well and notice how I could
08:24literally change stuff as I'm going sending it into that new date object and
08:29just to make sure that I could see that everything is working just for a second
08:33I'm going to return zero because it's expecting to get something and I'm just
08:37going to do a console dot log for our date because I just want to see that our
08:42date really is October 19th 1982 so if I do you're a little refresh we'll see
08:49that this is exactly the value we were expecting so let's continue so the
08:52next step is I actually want to get the amount of seconds that pass since that
08:56birthday to make that happen I'm going to have to create another date object
09:00and I'm going to call it now and to do that I'm going to create a new date object
09:04that returns to me the exact moment in time of right now because I want to
09:08subtract the amount of time that passed between now and that birthday so to do
09:14that I'm going to approach a property of the date object which is called get
09:18time now the get time function what it does is returns to us the amount of
09:23milliseconds that passed since 1969 and you can see here this is that that value
09:29of amount of times that pass amount of milliseconds that passed since 1969 but
09:34we don't really care about 1969 what we care is about the milliseconds that
09:38passed since my birthday so what we want to do is we want to subtract the
09:44amount of time between that passed from now and my birthday so literally I'm
09:50gonna take the now time get the amount of time that passed since 1969 and
09:55subtract the amount of time that passed from 1969 as well now the leftover that
10:01will be left would be the amount of time that passed since the day that I
10:04was born so if I take that milliseconds amount of milliseconds that
10:07passed and I just run this amount of milliseconds into our log just to see
10:11the value that passes we'll see that now we see the amount of milliseconds that
10:16passed now we're not ready yet because our function is expecting us to send the
10:20amount of seconds that passed since my birthday so we still need to divide this
10:24value by a thousand so if I take here the amount of seconds we want to take
10:28the amount of milliseconds and divide it by a thousand because there's a thousand
10:32milliseconds in a second and if I trace out that second we'll have a little
10:37problem and that's because we're gonna have a leftover so if we go ahead and go
10:41into our browser and click on refresh we're gonna also have a leftover which
10:45is a problem so we want to make sure that we get rid of this and the best way
10:49for us to get rid of it is go back to that built-in object that we talked
10:53about of the math object and see if there's a function that will help us
10:57clean things up and there definitely is and there's a function in the math
11:01class that just called round and our round function will take whatever value
11:05is there and round it down into an integer so if I save this and run my
11:09function one more time one my application one more time we'll see that
11:12now we're getting the amount of seconds and we are beautifully set all that's
11:17left for us to do is to return that value so I'm going to go ahead and send
11:21that value as the return instead of zero and last but not least I'm gonna
11:26grab this narf text and I'm just gonna push push it right underneath after we
11:32got our seconds and I'll write here the second since your birthday are and I'll
11:41just put your plus and I'll just add here the amount of seconds and if we run
11:47it let's make it even a little bit shorter so in case because I'm sure
11:52there's not gonna be a lot of room to second since your birthday since your
12:00birth how about that we're saving a few more characters and if I just save this
12:05and I go back into my application and click on refresh we'll see that our
12:08application will start up with the second since your birth as you know it's
12:13a little bit behind there which kind of so how about we'll change this to the
12:19amount of seconds plus birth with a space there so we will have a space
12:31between that big number and the amount of seconds that pass all right so
12:36obviously this is not as intuitive but you know what it means and I know what
12:39it means and this means that I'm quite old because over a million I'm a
12:43library for over a million seconds that's kind of cool I think it's even
12:49more than that Wow so I'm a million seconds old so happy 1 million second
12:55birthday to me in the next lecture I want to continue this just for the
12:59sake of practice I want us to also get the amount of hours that pass since my
13:04birth so how about we'll also underline that copy underneath here and maybe put
13:08here a few lines such as the amount of seconds is my birth the amount of hours
13:12since my birth the amount of days since my birth just in case someone cares
13:17about it maybe you do maybe I do so that's it we'll see in the next lesson
13:22when we look at that as just a small practice and before we move on to the
13:27next big topic in objects so see you in the next lesson

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!