The date object is a very special object. It allows you to grab the current time and date. It also allows you to manipulate the time and date easily. Let’s take a look at how useful the date object can be.
In order to use the date object, you have to store the instance of the object inside a variable. What’s an instance? An instance is a copy of an object. It’s not the original object itself. By doing this, anything you do to one instance, will not harm the original object or other instances. For example, you can open up 2 notepads and they will both hold totally different data. They’re both the same program, but you can type and save different text. Both notepads are considered instances of the notepad program.
Let’s take a look at how to create an instance of the date object.
var d = new Date();
The new keyword allows you to create an instance of an object. You then specify what object you would like an instance of. In our case, we want an instance of the date object. Now, you can grab the current date and time. Let’s take a look at how to do this.
Using The Date Object
After the line of the creation of the object, add this bit of code.
document.write( d + '<br>' );
By simply outputting the variable itself, the whole date is presented. Depending on the day and time you’re viewing this tutorial, you’ll always see a different and unique time.You should get something similar to this format.
Thu Jun 27 2013 19:47:42 GMT-0700 (Pacific Daylight Time)
This is pretty helpful, but what if you wanted to get a specific time or day or month? Well, the date object doesn’t have any properties that are useful, but it has a ton of methods to grab this kind of data.
Add this bit of code into your script.
document.write( d.getDate() + '<br>' );
document.write( d.getFullYear() + '<br>' );
document.write( d.getHours() + '<br>' );
document.write( d.getMinutes() + '<br>' );
document.write( d.getTime() + '<br>' );
These are a couple of methods to grab certain aspects of the current time. If you refresh your page, you should get something similar to this.
Let me explain what each method does
- getDate() – This method will return the current day.
- getFullYear() – This method will return the current year.
- getHours() – This method will return the hour( 0 – 23 )
- getMinutes() – This method will return the minute
- getTime() – This method will return the amount of milliseconds since midnight Jan 1, 1970
You can grab other data as well. What if you wanted to modify the current time or date? Say 5 minutes ago or 3 years ago? This is possible and very easy to do.
Let’s take a look at how to do this. Add this bit of code into your script.
d.setDate( 15 );
document.write( d.getDate() );
Very similar to the get methods, the set methods will allow you to modify the time. In the example, I only modify the day, You simply pass in the value you want to change it to. Then, I output it into the document and you should see the day changed.
There are so many things you can grab and modify. The date object is one of the few objects you’ll find yourself using often. If you would like to see a complete list of methods you can use, then go here.