The JavaScript Date Object

You often need to work with the dates in your web projects for different purposes. Some date dependent work can be done on the server side while other needs to be done at client side.

For example, only retrieving today’s messages from the inbox. Getting the report for given dates from the user, displaying a clock and so on.

JavaScript date object is the way to work with dates in your project. The JS date has built in functions to format dates. In this tutorial, we will explain how to create date object along with examples.

JavaScript date syntax

The date object can be created by using the new Date keyword. Following is the general syntax of creating a date object:

var jsDate = new Date();

You may use optional parameters inside the date as well. The parameters include milliseconds, dateString etc. Using Date object without parameters will return current date and time with the timezone. See examples below of using date object with date functions.

JavaScript current date example

Following is an example of using the date object. We simply created a date variable and assigned it Date object by using the new Date keyword. As you click on the button, the current date and time will be shown in an alert. See example and code by clicking the link below:

Experience this example online

As you can see the current date is shown in an alert.

Javascript date functions

In the above example, we simply used date javascript object and displayed the current date. Let us look at a few useful date functions using the date object. These functions include getting only the day, month, year rather complete date and time. Following are examples of using these date functions.

A getDay function example

The example shows day number of the Week (as an integer) by using the JavaScript getDay function. Where 0 is for Sunday and 6 for Saturday. See example by clicking the link or image below:

Experience this example online

In the demo, the number of the day in Week is shown as you click the button. Also note, the number of the day is based on the local system while using the getDay method.

You may wonder why it starts from 0 rather 1 for Sunday and same happens for Month number i.e. 0 for Jan, 11 for Dec as using the date object. The reason is, as date functions return numeric values and to get Month name (Jan, Feb…) or day name (Sat, Sun,…) you have to use arrays. As array index starts from 0, this will reduce coding effort.

The getDate method example

The following example uses JavaScript getDate method of the date object. The getDate returns date of the Month. See the example by clicking the link:

Experience this example online

getFullYear example

The getFullYear method of date JavaScript returns full year for a given date. The returned value is four digit value (full year).

Experience this example online

A getMonth example

The following example uses JS getMonth method to return the number of the Month. The 0 represents January and 11 for December. The example will show the current month on your local system as you click on the button.

Experience this example online

As you can see, the date method is showing current month number.

JavaScript date formatting

Until now we learned how to use the date in JavaScript along with a few methods of the date object. One other aspect of working with the date is formatting dates. Due to its importance, we have covered this in a separate chapter. Click here to go to date format chapter.

Also see JavaScript array | Date formatting