Creating The Date Object
Although we probably first learn how to create a basic object i wanted to start our learning with a real life example in our next title we will dig deeper into how to objects work and interact with creating our own Object.
Video Transcript
00:00All righty, so congratulations.
00:01You know now how to work with some basic objects.
00:04The next step is we want to make sure,
00:06well, we want to start doing a little bit of practice
00:08in a real kind of real case scenario.
00:11So if you look into our sample code
00:13and you could find our sample code in your source files
00:16and actually we're going to be working on our index end
00:19because we're going to be using this as our end project.
00:23Now what I want to do is I want to change this area here,
00:26this bar that has only one element that's visible.
00:30What I actually want to do is via code, via JavaScript,
00:33try to figure out which one should be active
00:36and based on the one that needs to be active
00:38based on the current date,
00:39we'll then just have that selection highlighted
00:42and dim out the other ones,
00:45basically making sure that only one will be visible
00:47just as it looks like right now.
00:49Only the two others will be dimmed out
00:52including our rabbits.
00:54All right, so let's get rolling.
00:56So first of all, we'll open up our file index.end
00:59in our Dreamweaver
01:02or whatever application you choose to work with.
01:04Now, the first step that I want to do
01:06is I want to locate that element.
01:09And to locate the element is actually
01:10I have here a couple of boxes, which are right here.
01:14And those boxes, you could see that
01:15some point around here, you see the transparent.
01:17So I'm just going to delete all the extra information
01:20that's coming with our text area,
01:22making sure that our transparency won't be there anymore.
01:26And if I set it up to live view
01:29and we look at the design itself,
01:31we'll see that now all three elements are not dimmed out.
01:34Perfect.
01:35So the next step for us is now that we got that sorted out,
01:38the next step for us is, and by the way,
01:40first of all, before we do that,
01:41do you remember that transparent?
01:43So we need to remember that class transparent
01:45because we will be using it.
01:47We're just not going to start off with it.
01:49All right.
01:50And what we're actually going to focus on,
01:52we're going to be focusing on the boxes themselves.
01:55Now, while you were gone, I added an ID to each element.
01:59So you can see that our divs have a early date,
02:02mid date, last date.
02:04So all our three components have an ID element.
02:08So let's go on and start doing some code.
02:11And to do the code, we first want to move our JavaScript
02:13to be our final JavaScript.
02:15So I'm going to change it to final JavaScript.
02:17I'm going to save that and refresh
02:20and click into the JavaScript code
02:22so we could get into coding.
02:23And just as we did before,
02:25what I want to do is as soon as our body is loaded,
02:28we want to call our on load,
02:30oh, not on blur, on load.
02:37And we want to call our init function.
02:39We're going to start our function up by setting it up.
02:41And now it's time for us.
02:43So just a reminder, on load equals
02:45and you put whatever JavaScript code you want to put in.
02:47And in our case, we want to create a new function.
02:50We want to call it init and we want to start working.
02:55Now, the first step that we want to do
02:56before we even start manipulating our HTML itself
02:59is figuring out what is today's date.
03:01We can figure that out very easily using our date object,
03:05which is there is a object in JavaScript.
03:08Now, the way you create new objects,
03:10we haven't done that so far.
03:11So far, we've only worked with objects
03:13that were created already previously.
03:16In this case, I want to create a date object.
03:18Now, to create a date object in JavaScript,
03:21it works this way.
03:22You say the word new and then space.
03:25And this is true, by the way,
03:26to every single type of object.
03:27And we're going to look much more deeper into this
03:30in a couple of more videos.
03:32So we're going to approach one of the objects
03:35that already exist.
03:35And one of these objects are called date.
03:38When I create a new date
03:39and I just set it up to be the default date
03:41as it is right now,
03:43you'll see that first of all, when I create it,
03:45if I try to do an alert box
03:47and I alert that date, that variable that I created,
03:50and by the way, before we do that, step by step.
03:52So first of all, I'm creating a variable
03:55and I'm saying new date.
03:56And it's really important to notice the case sensitivity.
03:59So in our case, it always starts
04:01with a uppercase D, date.
04:04And then we open and close a curly bracket,
04:07very similar to the way that we worked with functions.
04:10And it's not a coincidence as you get more familiar
04:13with how objects are being created.
04:15It's actually an invisible function
04:17that actually is creating this object.
04:20But so in our case, we're creating a new date.
04:22Now that we have our variable date,
04:24that date by default is going to contain our current date.
04:28So if I refresh our application,
04:29we'll see that currently we're,
04:31while I'm doing this recording,
04:32we're on April 11th, it's the year 2012,
04:36and the time now is 4 p.m.
04:39All right, so the next step,
04:40now that we have our current date,
04:42we want to start creating three more dates.
04:44A date for our mid,
04:47and if we look at our design for a second,
04:50we want to have a date for,
04:51first of all, everything that's before a specific date
04:53will be the early bird.
04:57From that specific date until our late date,
05:00it would be the late price.
05:02So in our case, what we want to do is,
05:04we want to make sure that we have that 315
05:07and we have, make sure that we have that 415,
05:09but we're going to make it a little bit different
05:11because 315, 415 is not extremely valuable to us
05:16because we are already,
05:18oh, it actually is working well in April.
05:21We're in April now,
05:22and that means that we're already in the middle between,
05:29we're in the middle zone.
05:31That means our early bird will never happen.
05:33We're in the middle zone there between our two dates.
05:36All right, so what I want to do next
05:39is I want to show you how to work with the date objects.
05:42Now, a way to create a date,
05:44then we know exactly what date we want.
05:47So let me show you an example.
05:49So in our case, we want to create a date
05:51and we know exactly what date we want it to be.
05:54We could go to our date and we could set things.
05:57So we could set our specific date itself.
05:59We could set our month, we could set our year.
06:02In our case, we're only going to set the month itself.
06:06So I'm going to create this date
06:08and just we're going to rename
06:09a couple of stuff here, first of all, okay?
06:11So instead of calling this date, we're going to call it now.
06:15I'm going to create another date
06:16and I'm going to call it early date.
06:22Early date is going to be a new date as well.
06:24So we're doing a little bit of practice here.
06:26I'm going to go into early date
06:29and I'm going to set the set month to be,
06:33I'm going to set the month to be, let's start with zero.
06:36I want to show you something.
06:37If I set the month to zero,
06:39oh, I did get month instead of set month.
06:41So I'm going to do set month
06:44and I'm going to do here a little alert
06:46and I'm going to alert our early date
06:48just to see what date we get back.
06:50What is zero?
06:51What does zero equal to?
06:53And it's not going to be extremely logical to you right now
06:56that zero is actually January
06:58and that one is not January, it's actually February.
07:04But I want you to bear with me on this one.
07:05In the meantime, there's something that's called a raise
07:07which we didn't start learning yet.
07:09It's a topic that we're going to be covering
07:10very, very soon.
07:12But basically, anything that's a list,
07:15there's lists that are numeric lists
07:16when you're coming to JavaScript
07:18and you're working with a lot of programming languages,
07:20numeric list starts from zero.
07:22So in our case, behind the scenes,
07:24because we start from zero,
07:25our basically our first month is zero,
07:27thus our 12 month would be 11.
07:30Now, in our case, because we want to target our 315,
07:34we'll basically set our month to be two
07:36because two would be our third month.
07:39And same is true, early date, set date.
07:50And we want to set the date itself to be 15.
07:53And we're going to do that, we're going to alert that
07:55and just make sure that we're getting the 15th of the month
07:57and we're not getting a different date.
07:58And we absolutely are, we're getting March 15th
08:01which is exactly the date that we want to get.
08:03So we have here our early date.
08:06I want to create another variable
08:07and this is going to be our late date.
08:11It's going to be a new date as well.
08:14Our late date we want to set up in the very, very similar way
08:18as we've done it before.
08:19So if we look at our dates that we decided earlier,
08:22let me just comment down the alert box for a second here.
08:25We'll see that our late date is basically starts on 415
08:29which is one month exactly after our 315 one.
08:33So all I have to do is push this up to three.
08:36And there we go, this is all the preparation
08:38that we needed to do to start getting rolling.
08:40We now know what is our current date.
08:42We know what the early date is.
08:44We know what the late date is.
