Understanding JavaScript Date Methods
Date 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.
- Constructor ‘new Date ( )’ creates a Date object set to current date and time.
- Constructor ‘new Date (milliseconds_numeric_value)’ creates a Date object with date and time starting from Jan 01, 1970 UTC plus a value in ‘milliseconds_numeric_value’ argument.
- Constructor ‘new Date (date_string)’ creates a Date object with date specified in date_string which have a valid format accepted by ‘Date.parse’ method.
- Constructor ‘new Date (year, month, day, hour, minute, second, millisecond)’ creates a Date object using seven arguments provided in construction.
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:
- ‘setFullYear (year [, month, date])’ sets the ‘year’ (recommend four digits) of a date object. Arguments ‘month’ and ‘date’ are optional.
- ‘setMonth (month [, date])’ sets the zero based ‘month’ of a date object. Argument ‘date’ is optional.
- ‘setDate (date)’ sets the ‘day of the month’ of a date object.
- ‘setHours (hour [, minute, second, milliseconds])’ sets the ‘hour’ of a date object. Arguments ‘minute’, ‘second’ and ‘milliseconds’ are optional.
- ‘setMinutes (minute [, second, milliseconds])’ sets the ‘minute’ of a date object. Arguments ‘second’ and ‘milliseconds’ are optional.
- ‘setSeconds (second [, milliseconds])’ sets the ‘second’ of a date object. Argument ‘milliseconds’ is optional.
- ‘setMilliseconds (milliseconds)’ sets the ‘milliseconds’ of a date object.
- ‘setTime (milliseconds)’ sets a date and time by adding or subtracting the specified milliseconds with a starting point of Jan 01, 1970.
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:
- ‘getFullYear ()’ returns the ‘year’ (four digits) of a date object.
- ‘getMonth ()’ returns the zero based ‘month’ of a date object.
- ‘getDate ()’ returns the ‘day of the month’ of a date object.
- ‘getDay ()’ returns the ‘day of the week’ of a date object.
- ‘getHours ()’ returns the ‘hours’ of a date object
- ‘getMinutes ()’ returns the ‘minutes of a hour’ of a date object.
- ‘getSeconds ()’ returns the ‘seconds of a minute’ of a date object.
- ‘setMilliseconds ()’ returns the ‘milliseconds of a current second’ of a date object.
- ‘getTime ()’ returns the ‘numbers of milliseconds’ since Jan 01, 1970 midnight.
- ‘getTimezoneOffset ()’ returns the ‘number of minutes’, a time difference between UTC and local time.
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.
- ‘toString ()’ converts the Date object into a string.
- ‘toDateString ()’ converts the’ date portion’ of a Date object into a string.
- ‘toTimeString ()’ converts the’ time portion’ of a Date object into a string.
- ‘toUTCString ()’ returns the date as a string in universal time.
- ‘toISOString ()’ returns the date as a string in ISO standard form.
- ‘toJSON ()’ returns the date as a string in JSON format.
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.
Recommended Articles
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.