JavaScript Date Object
JavaScript Date Object |
Date ObjectThe Date object is used to work with dates and times. 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:
my_date.getDate() |
You can also write a date inside the parentheses of the Date() object, like this:
new Date(“Month dd, yyyy hh:mm:ss”) new Date(“Month dd, yyyy”) new Date(yy,mm,dd,hh,mm,ss) new Date(yy,mm,dd) new Date(milliseconds) |
Here is how you can create a Date object for each of the ways above:
var my_date=new Date(“October 12, 1988 13:14:00”) var my_date=new Date(“October 12, 1988”) var my_date=new Date(88,09,12,13,14,00) var my_date=new Date(88,09,12) var my_date=new Date(500) |
The Date object’s methods are described below:
NN: Netscape, IE: Internet ExplorerMethods
Syntax: object.method_name()
Method | Description | NN | IE |
---|---|---|---|
Date() | Returns a Date object | 2 | 3 |
getDate() | Returns the date of a Date object (from 1-31) | 2 | 3 |
getDay() | Returns the day of a Date object (from 0-6. 0=Sunday, 1=Monday, etc.) | 2 | 3 |
getMonth() | Returns the month of a Date object (from 0-11. 0=January, 1=February, etc.) | 2 | 3 |
getFullYear() | Returns the year of a Date object (four digits) | 4 | 4 |
getYear() | Returns the year of a Date object (from 0-99). Use getFullYear instead !! | 2 | 3 |
getHours() | Returns the hour of a Date object (from 0-23) | 2 | 3 |
getMinutes() | Returns the minute of a Date object (from 0-59) | 2 | 3 |
getSeconds() | Returns the second of a Date object (from 0-59) | 2 | 3 |
getMilliseconds() | Returns the millisecond of a Date object (from 0-999) | 4 | 4 |
getTime() | Returns the number of milliseconds since midnight 1/1-1970 | 2 | 3 |
getTimezoneOffset() | Returns the time difference between the user’s computer and GMT | 2 | 3 |
getUTCDate() | Returns the date of a Date object in universal (UTC) time | 4 | 4 |
getUTCDay() | Returns the day of a Date object in universal time | 4 | 4 |
getUTCMonth() | Returns the month of a Date object in universal time | 4 | 4 |
getUTCFullYear() | Returns the four-digit year of a Date object in universal time | 4 | 4 |
getUTCHours() | Returns the hour of a Date object in universal time | 4 | 4 |
getUTCMinutes() | Returns the minutes of a Date object in universal time | 4 | 4 |
getUTCSeconds() | Returns the seconds of a Date object in universal time | 4 | 4 |
getUTCMilliseconds() | Returns the milliseconds of a Date object in universal time | 4 | 4 |
parse() | Returns a string date value that holds the number of milliseconds since January 01 1970 00:00:00 | 2 | 3 |
setDate() | Sets the date of the month in the Date object (from 1-31) | 2 | 3 |
setFullYear() | Sets the year in the Date object (four digits) | 4 | 4 |
setHours() | Sets the hour in the Date object (from 0-23) | 2 | 3 |
setMilliseconds() | Sets the millisecond in the Date object (from 0-999) | 4 | 4 |
setMinutes() | Set the minute in the Date object (from 0-59) | 2 | 3 |
setMonth() | Sets the month in the Date object (from 0-11. 0=January, 1=February) | 2 | 3 |
setSeconds() | Sets the second in the Date object (from 0-59) | 2 | 3 |
setTime() | Sets the milliseconds after 1/1-1970 | 2 | 3 |
setYear() | Sets the year in the Date object (00-99) | 2 | 3 |
setUTCDate() | Sets the date in the Date object, in universal time (from 1-31) | 4 | 4 |
setUTCDay() | Sets the day in the Date object, in universal time (from 0-6. Sunday=0, Monday=1, etc.) | 4 | 4 |
setUTCMonth() | Sets the month in the Date object, in universal time (from 0-11. 0=January, 1=February) | 4 | 4 |
setUTCFullYear() | Sets the year in the Date object, in universal time (four digits) | 4 | 4 |
setUTCHour() | Sets the hour in the Date object, in universal time (from 0-23) | 4 | 4 |
setUTCMinutes() | Sets the minutes in the Date object, in universal time (from 0-59) | 4 | 4 |
setUTCSeconds() | Sets the seconds in the Date object, in universal time (from 0-59) | 4 | 4 |
setUTCMilliseconds() | Sets the milliseconds in the Date object, in universal time (from 0-999) | 4 | 4 |
toGMTString() | Converts the Date object to a string, set to GMT time zone | 2 | 3 |
toLocaleString() | Converts the Date object to a string, set to the current time zone | 2 | 3 |
toString() | Converts the Date object to a string | 2 | 4 |