JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)
Chapter 20. Dates and Time
The Date Object
The data object enables basic storage and recall of dates and times. Below is the syntax for the date object:
Below are the parameters of the date object:
Parameter |
Description |
dateObi |
Mandatory. This is used to assign the Date object. |
dateVal |
Mandatory. When a numeric value, this parameter signifies the number of milliseconds in UCT between the certain time and Jan 1 1970 midnight. If dateVal is a string, this will be parsed based on the rules in JavaScript. Meanwhile, the dateVal argument could also be a VT_DATE value as generated from certain objects ActiveX. |
ms |
Optional. Refers to the number between zero to 999, which specifies milliseconds. |
seconds |
Optional. Should be included if you add milliseconds. This refers to the number from zero to 59, which signifies the seconds. |
minutes |
Optional. Should be included if you add seconds. This refers to the number from zero to 59, which signifies the minutes. |
hours |
Optional. Should be included if you add minutes. This refers to the number from zero to 23 (mn to 11:00 pm, which refers to the hour) |
month |
Mandatory. The month is a number from zero to 11 (Jan to Dec) |
year |
Mandatory. Should be complete year such as 1996 and not 96 |
date |
Mandatory. The date as an integer in between one and 31. |
Take note that a data object will contain a number signifying a certain occurrence in time to within a millisecond. When the value of an argument is larger than the range or this is a negative number. Other stored values will be modified. For instance, if you enter 240 seconds, the JavaScript will redefine the number as four minutes.
When the number is NaN, the object will not represent a particular time instant. When you pass no parameters to the Date object, it will be initialized to UTC. Remember, a value should be provided to the object before using this. The date range could be signified in the object Date about 285616 years before and after Jan 1 1970.
The example below shows the use of the Date object.
Date Object Properties
There are two properties of the Date Object: prototype Property and constructor Property.
Property |
Description |
Prototype |
Yield a reference to the objects class prototype |
Constructor |
Specifies the function that creates an object |
Date Object Functions
Functions |
Description |
Date.UTC |
Yields the number of milliseconds between Jan 1, 1970 UTC and the date added |
Date.parse |
Parses that string that contains a date, and yields the milliseconds between the date and midnight Jan 1, 1970 UTC |
Date.now |
Yields the milliseconds between Jan 1, 1970 and the present time and date |
Date Object Methods
Below is the list of Date object methods:
Method |
Description |
getFullYear |
Yields the year value in local time. |
getHours |
Yields the hours value in local time. |
getDay |
Yields the week day in local time. |
getDate |
Yields the month day through local time |
getMinutes |
Yields the minute value in local time |
getMonth |
Yields the month value in local time |
getSeconds |
Yields the seconds value in local time |
getMilliseconds |
Yields the milli seconds in local time |
getTime |
Yields the time within an object Date as the no. of milli seconds since Jan 1, 1970 midnight |
getUTCDate |
Yields the month day in UTC |
getUTCDay |
Yields the week day in UTC |
getUTCFullYear |
Yields the year in UTC |
getUTCHours |
Yields the hours in UTC |
getUTCMilliseconds |
Yields the milliseconds in UTC |
getUTCMinutes |
Yields the minutes in UTC |
getUTCMonth |
Yields the month in UTC |
getSeconds |
Yields the seconds in UTC |
getTimezoneOffset |
Yields the minute difference between the UTC and the computer |
getYear |
Yields the value year |
getVarDate |
Yields the value VT_DATE in a Date object |
isPrototype |
Yields a Boolean value, which signifies if an object is existing in the prototype chain of an object |
hasOwnProperty |
Yields the Boolean value, which signifies if an object has a property with the particular name |
propertyIsEnumerable |
Yields a Boolean value, which signifies if a certain property is a composition of an object and if it is enumerable. |
setSeconds |
Defines the seconds value in local time |
setMonth |
Defines the month value in local time |
setMinutes |
Defines the minute value in local time |
setMilliseconds |
Defines the milliseconds value in local time |
setHours |
Defines the hour value in local time |
setDate |
Defines the numerical month day in local time |
setFullYear |
Defines the value year in local time |
setUTCSeconds |
Defines the seconds value in UTC |
setUTCMonth |
Defines the month value in UTC |
setUTCMinutes |
Defines the minutes value in UTC |
setUTCMilliseconds |
Defines the milliseconds value in UTC |
setUTCHours |
Defines the hours value in UTC |
setUTCFullYear |
Defines the year in UTC |
setUTCDate |
Defines the numerical month day in UTC |
setTime |
Defines the date and time value in object Date |
setYear |
Defines the year value in loc time |
toLocaleTime |
Yields a time as a value string suitable to the current local of the host environment |
toLocaleDateString |
Yields a data in string value suitable to the current local of the host environment |
toDateString |
Yields a data in value string |
toISOString |
Yields a date in value string but in ISO format |
toGMTString |
Yields a date changes to a string in GMT |
toJSON |
Yields a changed data of type object prior to the JSON serial |
valueOf |
Yields the prime value of a defined object |
toTimeString |
Yields the time as a string value |
toString |
Yields a string representation of the object |
toUTCString |
Yields a date changed to a string in UTC |
Calculating Dates and Time using JavaScript
It is possible to use the object Date in order to perform usual clock and calendar tasks like computing elapsed time and date comparison.
Setting up Date to the Existing Date
If you make an instance of the object Date without defining a date, it will return a value, which will signify the present date and time, which also includes the millisecond, the second, the minute, hour, day, and year. You can then modify or read this date and time.
The example below shows the process of instantiating a date without the use of any type of parameters and showing the result in mm-dd-yy format.
Setting up a Particular Date
It is possible to set a particular date through passing string date to the constructor.
The time zone shown in the date string is suitable to the time zone defined in the local machine. It is a good thing that JS is dynamic about the string format used as the parameter. For instance, you can use “1-16-2016”, “January 16, 2016”, or “16 Jan 2016”.