JavaScript Class
The Date object
The Date object is used to work with dates and times. More specifically, the Date object allows you to work programatically/logically with dates and times.
Just like with an Array, you create an instance of the Date object with the "new" keyword.
To store the current date in a variable called "my_date":
After creating an instance of the Date object, you can access all the methods
of the object from the "my_date" variable. If, for example, you want to return
the date (from 1-31) of a Date object, you should write the following:
You can also write a date inside the parentheses of the Date() object, if you want:
Examples:
The Date object's methods are described below:
NN: Netscape, IE: Internet Explorer, ECMA: Web
Standard
Methods |
Explanation |
NN |
IE |
ECMA |
Date() |
Returns a Date object |
2.0 |
3.0 |
1.0 |
getDate() |
Returns the date of a Date object (from 1-31) |
2.0 |
3.0 |
1.0 |
getDay() |
Returns the day of a Date object (from 0-6. 0=Sunday, 1=Monday, etc.) |
2.0 |
3.0 |
1.0 |
getMonth() |
Returns the month of a Date object (from 0-11.
0=January, 1=February, etc.) |
2.0 |
3.0 |
1.0 |
getFullYear() |
Returns the year of a Date object (four digits) |
4.0 |
4.0 |
1.0 |
getYear() |
Returns the year of a Date object (from 0-99). Use getFullYear
instead !! |
2.0 |
3.0 |
1.0 |
getHours() |
Returns the hour of a Date object (from 0-23) |
2.0 |
3.0 |
1.0 |
getMinutes() |
Returns the minute of a Date object (from 0-59) |
2.0 |
3.0 |
1.0 |
getSeconds() |
Returns the second of a Date object (from 0-59) |
2.0 |
3.0 |
1.0 |
getTime() |
Returns the number of milliseconds since midnight 1/1-1970 |
2.0 |
3.0 |
1.0 |
setDate() |
Sets the date of the month in the Date object (from 1-31) |
2.0 |
3.0 |
1.0 |
setFullYear() |
Sets the year in the Date object (four digits) |
4.0 |
4.0 |
1.0 |
setHours() |
Sets the hour in the Date object (from 0-23) |
2.0 |
3.0 |
1.0 |
setMinutes() |
Set the minute in the Date object (from 0-59) |
2.0 |
3.0 |
1.0 |
setMonth() |
Sets the month in the Date object (from 0-11. 0=January,
1=February) |
2.0 |
3.0 |
1.0 |
setSeconds() |
Sets the second in the Date object (from 0-59) |
2.0 |
3.0 |
1.0 |
setYear() |
Sets the year in the Date object (00-99) |
2.0 |
3.0 |
1.0 |
|