Udemy logo

javascript date functionsDate creation, retrieval and representation are the most common operations in any software application. In JavaScript, Date objects are used for performing date operations.

What are JavaScript Date Methods?

JavaScript Date methods are used for creation, modification, parsing and formatting of a Date object. These methods include the ‘constructors’ to create a Date object, the ‘getters’ to retrieve the date information, the ‘setters’ to modify the Date object and ‘conversions’ to transform the date information in different formats.

To learn JavaScript in detail, take a course at Udemy.com.

Constructors in JavaScript Date Methods

Constructors are used to create the Date objects with ‘new’ keyword. Different variants of JavaScript Date constructors are as follows.

In the last constructor one thing should be kept in mind: the ‘month’ argument is zero-based meaning ’0’ represents the month of January. The following code snippet demonstrates the use of JavaScript Date constructors.

Code snippet:

var today_Date = new Date (); //Current date.

var Date_Milliseconds = new Date ( 24 * 60 * 60 * 1000 ); //Fri Jan 02 1970 05:00:00 GMT+0500 (West Asia Standard Time)

var Date_StringFormat = new Date ( “May 16, 2014 04:20:00” ); //Fri May 16 2014 04:20:00 GMT+0500 (West Asia Standard Time)

var Date_With_Arguments = new Date ( 2014, 04, 16, 04, 20, 0 ); //Fri May 16 2014 04:20:00 GMT+0500 (West Asia Standard Time)

Setters in JavaScript Date Methods

JavaScript Date Setter methods are used to change the date and time components of the Date object. The following methods are some important methods to know:

Following code snippet demonstrates the usage of the aforementioned methods:

Code snippet:

var sampleDate = new Date () //Sat May 17 2014 18:51:49 GMT+0500 (West Asia Standard Time)
sampleDate.setFullYear (2015);
sampleDate.setMonth (5);
sampleDate.setDate (2);
sampleDate.setHours (14);
sampleDate.setMinutes (44);
alert (sampleDate);//Tue Jun 02 2015 14:44:49 GMT+0500 (West Asia Standard Time)

In the above code snippet, the date object with current date is created. The call of ‘setFullYear’ method on Date object changes the year of the current date to ‘2015’ while the ‘setMonth’ method changes the zero based month part from current date object. Finally, ‘setDate’ changse the day of a month to ‘2’ and so on.

Interested in learning more about JavaScript? take a course at Udemy.com.

Getters in JavaScript Date Methods

JavaScript Date “getter” methods are used to retrieve the date and time components of the Date object. The following methods are some important ones to know:

Following code snippet shows the basic use of JavaScript Date Getter methods.

var sampleDate = new Date ();
alert (sampleDate.getFullYear ());
alert (sampleDate.getMonth ());
alert (sampleDate.getDate ());
alert (sampleDate.getDay ());
alert (sampleDate.getHours ());
alert (sampleDate.getMinutes ());
alert (sampleDate.getTime ());
alert (sampleDate.getTimezoneOffset ());

In most of the scenarios, a combination of JavaScript Date setter and getter is used. For example, you might need to add some days to a new registered user and set the resultant date as a trial period for that user. An example of this task is shown in the following code:

Code snippet:

var trialDate = new Date ();
trialDate.setDate (trialDate.getDate () + 5);
alert (trialDate);

In JavaScript Date methods, UTC variants of getters and setters exist that return the results according to the universal time with the exceptions for ‘getTime’, ‘setTime’ and ‘getTimezoneOffset’ methods.

Formats in JavaScript Date Methods

JavaScript Date Getter methods are used to change the date and time of the Date object to different formats, results in a string. Following are the important ones.

Code Snippet:

var sampleDate = new Date ();
alert (sampleDate.toJSON ()); // something like '2014-05-17T17:17:28.278Z'

In above code, JavaScript Date method ‘toJSON’ convert date object to a form of a JSON date string so that it can transfer with other JSON data.

Parsing in JavaScript Date Methods

The JavaScript Date method ‘parse’ is used to convert the string into the numbers milliseconds since Jan 01, 2014 midnight.

Code snippet:

var milliseconds = Date.parse ('2014-05-17T17:17:28.278Z');
var sampleDate = new Date (milliseconds);
alert (sampleDate);

In above code snippet ‘parse’ method of JavaScript Date is used to convert the date format in string to milliseconds and then a Date object is created with the returned milliseconds. If a ‘parse’ method failed to parse a string a ‘NAN’ value is returned.

To learn essentials of JavaScript, check out a course at Udemy.com.

Page Last Updated: May 2014

Top courses in JavaScript

JavaScript students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo