Tuesday , 18 June 2019
Home / Programming / JavaScript / Date Object – JavaScript Tutorial
Date Object
Date Object

Date Object – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

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.

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.

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.

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

  1. getDate() – This method will return the current day.
  2. getFullYear() – This method will return the current year.
  3. getHours() – This method will return the hour( 0 – 23 )
  4. getMinutes() – This method will return the minute
  5. 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.

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.


About Jasko Koyn

Check Also


Audio – Advanced JavaScript Tutorial

The video and audio objects both have the same events, properties and methods. So, you …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.