Javascript Dates reference
Javascript build-in Date object reference.
Create dates:
To create an object: | new Date(); new Date(milliseconds); new Date(datestring); new Date(year, month, day, hours, minutes, seconds, ms); |
A Date object in JavaScript is created with the new
operator and the Date() constructor in 4 possible ways:
|
|
To use as a function: | Date([param]) |
To use the constructor function, Date, as an function, then the function will return a string representation of the current date and time. In this case will all arguments be ignored. | |
An example of using the new Date([param]):
|
General information about the object member types:
- Prototype created methods, properties or constants can only be used on instances of an object or on a primitive datatype.
- Constructor created methods, properties or constants can NOT be used on instances of an object or on a primitive datatype.
Prototype Methods:
Syntax: | getTime() |
Converts date and time in a date object to a single integer that is a millisecond representation of the date object.
Millisecond representation of a date is independent of the time zone. |
|
Return value: | The millisecond representation of the adjusted Date object. |
An example of using the getTime() :
|
Syntax: | getTimezoneOffset() |
Returns the difference between local and universal (UTC) time, measured in minutes. | |
Return value: | A number of minutes. |
An example of using the getTimezoneOffset() :
|
Syntax: | getYear() |
Retrieves the year from a date object.
The getFullYear method is preferred for nearly all purposes, because it avoids the "year 2000 problem." |
|
Return value: | The year field of the specified Date object date minus 1900. |
An example of using the getYear() :
|
Syntax: | getDate() getUTCDate() |
Retrieves the day of the month from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | A value between 1 and 31 |
An example of using the get[UTC]Date() :
|
Syntax: | getDay() getUTCDay() |
Retrieves the day of the week from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | A value between 0 (Sunday) and 6 (Saturday). |
An example of using the get[UTC]Day() :
|
Syntax: | getFullYear() getUTCFullYear() |
Retrieves the year from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | A value that is a full four-digit year. |
An example of using the get[UTC]FullYear() :
|
Syntax: | getHours() getUTCHours() |
Retrieves the hour in a day from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | A value between 0 (midnight) and 23 (11 p.m.). |
An example of using the get[UTC]Hours() :
|
Syntax: | getMilliseconds() getUTCMilliseconds() |
Retrieves the millisecond field from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | An integer between 0 and 999. |
An example of using the get[UTC]Milliseconds() :
|
Syntax: | getMinutes() getUTCMinutes() |
Retrieves the minutes field from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | An integer between 0 and 59. |
An example of using the get[UTC]Minutes() :
|
Syntax: | getMonth() getUTCMonth() |
Retrieves the month from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | An integer between 0 and 11. It is 0 = January, 1 = february ... and so on. |
An example of using the get[UTC]Month() :
|
Syntax: | getSeconds() getUTCSeconds() |
Retrieves the seconds from a date object.
The returned value will be expressed in universal time if you are using UTC variant of the method. |
|
Return value: | An integer between 0 and 59. |
An example of using the get[UTC]Seconds() :
|
Syntax: | setTime(time) |
Sets the date and time with the milliseconds from the argument in a date object.
Representing a date in this millisecond format makes it independent of time zone. |
|
Return value: | The milliseconds argument. |
time: | The number of milliseconds between the desired date and time and midnight GMT on January 1, 1970. |
An example of using the setTime() :
|
Syntax: | setYear(year) |
Sets the year in a date object.
The setFullYear method is preferred for nearly all purposes, because it avoids the “year 2000 problem.” |
|
Return value: | The millisecond representation of the adjusted Date object. |
year: | An integer that is set as the year value (in local time) for a date object.
If this value is between 0 and 99, inclusive, 1900 is added to it and it is treated as a year between 1900 and 1999. |
An example of using the setYear() :
|
Syntax: | setDate(date) setUTCDate(date) |
Sets the day in the month in a date object.
The parameter value(s) ??should be in universal time if you are using UTC variant of the method. |
|
Return value: | The millisecond representation of the adjusted Date object. |
date: | An integer between 1 and 31 for the day in the month. |
An example of using the set[UTC]Date() :
|
Syntax: | setFullYear(year [,month [,date ]]) setUTCFullYear(year [,month [,date ]]) |
Sets the full year and optional month and date in a date object.
The parameter value(s) ??should be in universal time if you are using UTC variant of the method. |
|
Return value: | The millisecond representation of the adjusted Date object. |
year: | The year, expressed in local time. Should be an integer that includes the century, such as 2012. |
month: | An optional integer between 0 and 11 for the month. It is 0 = January, 1 = february ... and so on. |
date: | An optional integer between 1 and 31 for the daty in the month. |
An example of using the set[UTC]FullYear() :
|
Syntax: | setHours(hour [,min [,sec [,ms ]]]) setUTCHours(hour [,min [,sec [,ms ]]]) |
Sets the number of hours and optional minutes, seconds and milliseconds in a date object.
The parameter value(s) ??should be in universal time if you are using UTC variant of the method. |
|
Return value: | The millisecond representation of the adjusted Date object. |
hour: | One hour is given by an integer between 0 (midnight) and 23 |
min: | An optional minute is given by an integer between 0 and 59. |
sec: | An optional second is given by an integer between 0 and 59. |
ms: | An optional millisecond is given by an integer between 0 and 999. |
An example of using the set[UTC]Hours() :
|
Syntax: | setMilliseconds(ms) setUTCMilliseconds(ms) |
Sets the number of milliseconds in a date object.
The parameter value(s) ??should be in universal time if you are using UTC variant of the method. |
|
Return value: | The millisecond representation of the adjusted Date object. |
ms: | Millisecond is given by an integer between 0 and 999. |
An example of using the set[UTC]Milliseconds() :
|
Syntax: | setMinutes(min [,sec [,ms ]]) setUTCMinutes(min [,sec [,ms ]]) |
Sets the number of minutes and optional seconds and milliseconds in a date object.
The parameter value(s) ??should be in universal time if you are using UTC variant of the method. |
|
Return value: | The millisecond representation of the adjusted Date object. |
min: | Minutes is given by an integer between 0 and 59. |
sec: | An optional second is given by an integer between 0 and 59. |
ms: | An optional millisecond is given by an integer between 0 and 999. |
An example of using the set[UTC]Minutes() :
|
Syntax: | setMonth(month [,date ]) setUTCMonth(month [,date ]) |
Sets the month and optional a day in the month in a date object.
The parameter value(s) ??should be in universal time if you are using UTC variant of the method. |
|
Return value: | The millisecond representation of the adjusted Date object. |
month: | An integer between 0 and 11 for the month. It is 0 = January, 1 = february ... and so on. |
date: | An optional integer between 1 and 31 for the day in the month. |
An example of using the set[UTC]Month() :
|
Syntax: | setSeconds(sec [,ms ]) setUTCSeconds(sec [,ms ]) |
Sets the number of seconds and optional a number of millisecond in a date object.
The parameter value(s) ??should be in universal time if you are using UTC variant of the method. |
|
Return value: | The millisecond representation of the adjusted Date object. |
sec: | Seconds is given by an integer between 0 and 59. |
ms: | An optional millisecond is given by an integer between 0 and 999. |
An example of using the set[UTC]Seconds() :
|
Syntax: | toDateString() |
Returns an implementation-dependent, human-readable string representation of the date portion of date, expressed in the local time zone. | |
Return value: | A string representation of the date portion of a date object |
An example of using the toDateString() :
|
Syntax: | toLocaleDateString() |
Returns an implementation-dependent, human-readable string representation of the date portion of a date object, expressed in the local time zone and formatted according to local conventions. | |
Return value: | A string representation of the date portion of a date object |
An example of using the toLocaleDateString() :
|
Syntax: | toLocaleString() |
Converts a date to a string using the local time zone.
This method also uses the local conventions for date and time formatting, so that the format may vary from platform to platform and from country to country. |
|
Return value: | A string representation of the date and time of a date object based on the local time zone. |
An example of using the toLocaleString() :
|
Syntax: | toLocaleTimeString() |
Returns an implementation-dependent, human-readable string representation of the time portion of a date object, expressed in the local time zone and formatted according to local conventions. | |
Return value: | A string representation of the time portion of a date object |
An example of using the toLocaleTimeString() :
|
Syntax: | toString() toUTCString() toGMTString() |
Returns an implementation-dependent string that represents date.
The returned value will be expressed in universal time if you are using UTC variant of the method. The GMT variant of the method is the same as the UTC variant of the method. |
|
Return value: | A human-readable string representation. |
An example of using the to[UTC,GMT]String() :
|
Syntax: | valueOf() |
Converts date and time in a date object to a single integer that is a millisecond representation of the date object.
Millisecond representation of a date is independent of the time zone. |
|
Return value: | The millisecond representation of the adjusted Date object. |
Constructor (Date) Methods:
Syntax: | Date.parse(string) |
Parses the date contained in this string and returns it in millisecond format, which can be used directly to create a new Date object or set a date with the setTime() method. | |
Return value: | Milliseconds between the specified date and time and midnight GMT on January 1, 1970. |
string: | A string containing the date and time to be parsed. |
An example of using the parse() :
|
Syntax: | Date.UTC(year,month[,date[,hours[,minutes[,seconds[,ms ]]]]]) |
When the UTC function is called with fewer than two arguments, the behaviour is implementation-dependent.
When the UTC function is called with two to seven arguments, it computes the date from year, month and (optionally) date, hours, minutes, seconds and ms. UTC is Universal Time, which is similar to Greenwich Mean Time [GMT] |
|
Return value: | A time value as a number in milliseconds. |
An example of using the UTC() :
|
Prototype Properties:
Syntax: | Date.prototype.constructor |
This property of the Date.prototype holds the reference back to the Date object. | |
Important note: | This is the same as the constructor property of an instantiated object |
Constructor (Date) Properties:
Syntax: | Date.constructor |
The constructor property is a reference to the function that will be invoked to create a Date object. |
Syntax: | Date.prototype |
The prototype property is a reference to an object that contains all build-in methods that can be used by alle instances of Date-objects.
You can apply your own methods to the Date object through this prototype property. |
|
Important note: | Is not directly available on an instantiated object. |
Return value: | object |
An example of using the prototype :
|