|
 |
The Date object stores and displays dates and times.
There are four different formats for the Date object constructor. (1) If no arguments are provided, then the new Date object is set to the current date and time. (2) If a single numeric argument n is provided, then the object is set to January 1st, 1970 plus n milliseconds. (3) Multiple numeric arguments will set the year, month, day, etc. (4) Finally, the Date object constructor will accept a single string argument such as "Jan 8, 2001".
Dates stored can range from January 1, 1753, to December 31, 9999. If a date/time property is set to a value outside its range, the value will loop over its possible values and increment or decrement the appropriate associated property. Consider two examples: (1) Setting milliseconds to 1,300 will add 1 to the seconds count and 300 will be the value for milliseconds. (2) If date is set to 0, then the value stored for date is the last day of the previous month and the value stored in month decreases by 1.
Constructors
[new]
Date() |
Returns a new Date object instance set to the current date/time. |
[new]
Date(value) |
Returns a new Date object instance set to value number of milliseconds since January 1, 1970. |
[new]
Date(year, month[, date[,
hours[, minutes[, seconds[,
milliseconds]]]]]) |
Returns a new Date object instance initialized with the supplied arguments. See the Instance Methods below for valid ranges for each of the arguments. |
[new]
Date(date_string) |
Returns a new Date object instance initialized with the supplied
date_string. The syntax for the date_string argument has several variations. See the examples below for details. |
|
Instance Methods
format |
Provides custom display formats for dates and times. |
getDate |
Returns
an integer (1-31) indicating the day of the
stored date. |
getDay |
Returns
an integer (0-6, where 0 refers to Sunday)
indicating the day of the week
of the stored date. |
getHours |
Returns
an integer (0-23) indicating the
hour of the stored date. |
getMilliseconds |
Returns
an integer (0-999) indicating the
milliseconds of the stored date. |
getMinutes |
Returns
an integer (0-59) indicating the
minutes of the stored date. |
getMonth |
Returns
an integer (0-11) indicating the
month of the stored date. |
getSeconds |
Returns
an integer (0-59) indicating the
seconds of the stored date. |
getYear |
Returns
the four-digit integer indicating the year
of the stored date (1753-9999). |
setDate |
Sets
the integer (1-31) indicating the date of
the stored date. |
setFullYear |
Sets the year
and, optionally, the zero-based month and
non-zero-based date of the stored date. Quote enclosed full month names or 3-letter month abbreviations are also acceptable. |
setHours |
Sets the integer (0-23) indicating the hour and, optionally, the minutes (0-59), seconds (0-59) and milliseconds (0-999) of the stored date. |
setMilliseconds |
Sets the milliseconds of the stored date (0-999). |
setMinutes |
Sets the minutes (0-59) and, optionally, the seconds (0-59) and milliseconds (0-999) of the stored date. |
setMonth |
Sets the zero-based integer indicating the month (0-11) and, optionally, the non-zero-based date of the stored date (1-31). Quote enclosed full month names or 3-letter month abbreviations are also acceptable. |
setSeconds |
Sets the seconds (0-59), and optionally, the milliseconds (0-999) of the stored date. |
setYear |
Sets the year of the stored date (1753-9999). |
toString |
Returns
the stored date in ODBC-compatible
string format, for example, "1970-01-01 00:00:00". |
valueOf |
returns the current date/time in ODBC-compatible string format, for example, "1970 01-01 00:00:00" |
|
Example
The following example demonstrates the different constructors for the Date object.
var currentDate = new Date();
var date1 = new Date(-31536000000);
var date2 = new Date(1969, 0, 1);
var date3 = new Date("1/1/1969");
var date4 = new Date("Jan 1, 1969");
var date5 = new Date("Wednesday, January 1, 1969");
response.write
("Current Date: " + currentDate + "<br>" +
"date1: " + date1 + "<br>" +
"date2: " + date2 + "<br>" +
"date3: " + date3 + "<br>" +
"date4: " + date4 + "<br>" +
"date5: " + date5 + "<br>");
Output: (Note that the variable "currentDate" will be assigned a different value each time this code is run.)
Current Date: 2001-04-12 12:02:25
date1: 1969-01-01 00:00:00
date2: 1969-01-01 00:00:00
date3: 1969-01-01 00:00:00
date4: 1969-01-01 00:00:00
date5: 1969-01-01 00:00:00
|