Did you find this useful?
Socialize it today.


JavaScript Date Object

Friday, April 27, 2012

The JavaScript Date object allows you to work programmatically with dates and times. You create a Date object using the Date constructor. If you call the Date Object as a regular function without including the new operator, it will return a string rather than a Date object. There are four ways to initiate the Date object.

new Date()  
new Date(milliseconds)  
new Date(dateString)  
new Date(year, month, day [, hour, minute, second, millisecond ])


Date Parameters

Most parameters are optional. If you do not supply a parameter, 0 is passed. If any parameters are supplied, at a minimum, you have to include the year, month, and day. Note that if you only supply some parameters, those that are not specified are set to 0. Once a Date object is created, a number of methods allow you to operate on it. Most methods allow you to get and set the year, month, day, hour, minute, second, and milliseconds of the object, using either local time or Universal Time (UTC). Dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.


ParameterDescription

milliseconds

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC

dateString

String value representing a date. Format should follow IETF-compliant RFC 2822 timestamps.

year

Integer value representing the year. Always specify the full year; i.e. 2012, rather than 12.

month

Integer value representing the month, beginning with 0 for January to 11 for December.

day

Integer value representing the day of the month (1-31).

hour

Integer value representing the hour of the day (0-23).

minute

Integer value representing the minute segment (0-59) of a time reading.

second

Integer value representing the second segment (0-59) of a time reading.

millisecond

Integer value representing the millisecond segment (0-999) of a time reading.


Methods

Once the Date object has been created, you now have a variable that holds date information. To extract that information from the variable, we have to access it via a variety of methods.

MethodDescription

getDate()

Returns the day of the month (1-31) value using local time.

getDay()

Returns the day of the week (0-6) value using to local time.

getFullYear()

Returns the year (4 digits) value using local time.

getHours()

Returns the hour (0-23) value using local time.

getMilliseconds()

Returns the milliseconds (0-999) value using local time.

getMinutes()

Returns the minutes (0-59) value using local time.

getMonth()

Returns the month (0-11) value using local time.

getSeconds()

Returns the seconds (0-59) value using local time.

getTime()

Returns the time value in a Date Object as the number of milliseconds since 01/01/1970, 00:00:00 UTC.

getTimezoneOffset()

Returns the time-zone offset in minutes for the current locale.

getUTCDate()

Returns the day of the month (1-31) value using universal time.

getUTCDay()

Returns the day of the week (0-6) value using universal time.

getUTCFullYear()

Returns the year (4 digits) value using universal time.

getUTCHours()

Returns the hours (0-23) value using universal time.

getUTCMilliseconds()

Returns the milliseconds (0-999) value using universal time.

getUTCMinutes()

Returns the minutes (0-59) value using universal time.

getUTCMonth()

Returns the month (0-11) value using universal time.

getUTCSeconds()

Returns the seconds (0-59) value using universal time.

setDate()

Sets the day of the month (1-31) value using local time.

setFullYear()

Sets the full year (4 digits) value using local time.

setHours()

Sets the hours (0-23) value using local time.

setMilliseconds()

Sets the milliseconds (0-999) value using local time.

setMinutes()

Sets the minutes (0-59) value using local time.

setMonth()

Sets the month (0-11) value using local time.

setSeconds()

Sets the seconds (0-59) value using local time.

setTime()

Sets the Date object to the time represented by a number of milliseconds since 01/01/1970, 00:00:00 UTC.

setUTCDate()

Sets the day of the month (1-31) value using universal time.

setUTCFullYear()

Sets the full year (4 digits) value using universal time.

setUTCHours()

Sets the hour (0-23) value using universal time.

setUTCMilliseconds()

Sets the milliseconds (0-999) value using universal time.

setUTCMinutes()

Sets the minutes (0-59) value using universal time.

setUTCMonth()

Sets the month (0-11) value using universal time.

setUTCSeconds()

Sets the seconds (0-59) value using universal time.

toDateString()

Returns the "date" portion of the Date as a string.

toISOString()

Converts a date to a string following the ISO Format.

toLocaleDateString()

Returns the date portion of the Date as a string, using the current locale.

toLocaleString()

Converts a date to a string, using the current locale.

toLocaleTimeString()

Returns the time portion of the Date as a string, using the current locale..

toString()

Returns a string representing the specified Date object.

toTimeString()

Returns the time portion of the Date as a string.

toUTCString()

Converts a date to a string, using the universal time convention.

valueOf()

Returns the primitive value of a Date object.


JavaScript Date Examples

Here are some examples to help you better understand how to use the Date parameters and methods.

var today=new Date();  
var myDate=new Date("April 27, 2012 07:00:00");  
var myDate=new Date(2012,3,27);  
var myDate=new Date(2012,3,27,7,0,0);

Note that January = 0, not 1  


var myDate=new Date();
myDate.getDate();
myDate.setFullYear(2012,0,1);


Always keep in mind that JavaScript is case sensitive.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

JavaScript and jQuery: The Missing Manual Professional JavaScript for Web Developers Murachs JavaScript and DOM Scripting