The Date Object

Date objects are full script objects. The date is stored as the value of the date object and is the number of milliseconds.

Time is measured in milliseconds since 01 January, 1970 UTC (coordinated universal time). It is assumed that there are exactly 86,400,000 milliseconds per day. ECMAScript number values can represent all integers from –9,007,199,254,740,991 to 9,007,199,254,740,991; this range suffices to measure times to millisecond precision for any instant that is within approximately 285,616 years, either forward or backward, from 01 January, 1970 UTC.

Although the Date object is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.

Date objects are of class Date.

Date Object Properties
 

Property

Description

prototype

A reference to the Date Prototype. This property does not enumerate and can not be deleted but can be changed.

Date Object Functions

The Date object has no predefined functions.


The Date Constructor

Using the new Operator with the Date Constructor

The Date constructor is used to create new date objects. The constructor can have a various arguments.

var myDate = new Date();

A new date object is created set to the current time and date. This date object has a date is based on the timezone of the script server. This includes daylight saving.

var myDate = new Date(ms);

A new date is created set to the specified time in milliseconds.

var myDate = new Date(string[, language[, country]]);

A new date object is created for the date specified in the argument string supplied. The string is interpreted by the parse function (see below).

var myDate = new Date(year, month[, day[, hour[, minute[, second[, millisecond]]]]]);

A new date object is created using the numeric values of year, month, day, hour, minute, second and millisecond (where month is 0 to 11 but day is 1 to 31). The first two arguments are required but you can omit the other arguments from the this example. Any arguments not present will default to be zero. If the year is between 0 and 99, it will be added to 1900.

When a date object is created it is given the Date prototype. Shown below is the Date object prototype chain.

Calling the Date Constructor as a Function

When the Date constructor is run as a function without the new operator it returns a string representing the current date and time.

Date Constructor Properties

Constructor properties are read only (they can not be changed or deleted) and it is not possible to add new properties to the Constructor.

Property

Description

prototype

A reference to the Date Prototype. This property does not enumerate and can not be changed or deleted.

Date Constructor Functions
 

Function

Description

parse(string[, language[,country]])

The parse function converts its argument to a string and interprets the resulting string as a date; it returns a number, the UTC time value corresponding to the date. If language and country are present they are used to define the locale used to interpret the string, and if they are not present then the server JVM (Java Virtual Machine) locale is used. The string may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the string. If the string can not be interpreted then 0 is returned.

An example of a string for the English language is shown below:

"month day, year hours:minutes:seconds AM/PM" (January 27, 2001 9:30:00 AM)

In general the parse function will be able to parse the output of the toString function.

UTC(year, month[, date[, hours[, minutes[, seconds[, ms]]]]])

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 milliseconds.

Note: The UTC function differs from the Date constructor in two ways: it returns a time value as a number, rather than creating a Date object, and it interprets the arguments in UTC rather than as local time.


The Date Prototype

This prototype can be used by any object that has a numeric value.

Date Prototype Properties
 

Property

Description

prototype

A reference to the Object Prototype. This property does not enumerate and can not be deleted but can be changed.

Date Prototype Functions

The this reference used here always refers to the object that was used to run the function.

In all functions that accept or return year, month, date, hours, minutes, seconds and milliseconds values, the following representations are used:

A year is represented by an integer.

A month is represented by an integer form 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.

A date (day of month) is represented by an integer from 1 to 31.

An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.

A minute is represented by an integer from 0 to 59.

A second is represented by an integer from 0 to 61; the values 60 and 61 occur only for leap seconds and even then only in Java implementations that actually track leap seconds correctly. Because of the manner in which leap seconds are currently introduced, it is extremely unlikely that two leap seconds will occur in the same minute.

A millisecond is represented by an integer from 0 to 999 in the usual manner.

Except where explicitly stated otherwise all these functions are intentionally generic. They do not require that the this value be a Date object, and can therefore be applied to other objects where they will use the number value (see the Number Object) of the object.

Function

Description

getDate()

Return the day of the month (1 - 31).

getDay()

Return the day of the week (1 - 7).

getFullYear()

Return the year.

getHours()

Return the hour (0 - 23).

getMilliseconds()

Return the milliseconds (0 - 999).

getMinutes()

Return the minutes (0 - 59).

getMonth()

Return the month (0-11).

getSeconds()

Return the seconds (0 - 59).

getTime()

Returns a number, which is this time value.

This function is not generic. If the this object is not a date object then this object is returned.

getTimezoneOffset()

Returns the difference between local time and UTC time in minutes.

getYear()

The same as getFullYear().

getUTCDate()

The same as getDate().

getUTCDay()

The same as getDay().

getUTCFullYear()

The same as getFullYear().

getUTCHours()

The same as getHours().

getUTCMilliseconds()

The same as getMilliseconds().

getUTCMinutes()

The same as getMinutes().

getUTCMonth()

The same as getMonth().

getUTCSeconds()

The same as getSeconds().

parse(string)

DEPRECATED. This function has been moved to the Date Constructor object. It is still available from the Date Prototype for backward compatibility but may be removed in later releases.

setDate (date)

Sets the day of the month part of the current time. Return the new time.

setFullYear (year[, month[, date]])

Set the year part, the month part (if specified) and the day of the month part (if specified) of the current time. Return the new time.

setHours (hour[, min[, sec[, ms]]])

Set the hours part, the minutes part (if specified), the seconds part (if specified) and the milliseconds part (if specified) of the current time. Return the new time.

setMilliseconds (ms)

Set the milliseconds part of the current time. Return the new time.

setMinutes (min[, sec[, ms]])

Set the minutes part, the seconds part (if specified) and the milliseconds part (if specified) of the current time. Return the new time.

setMonth (month[, date])

Set the month part and the day of the month part (if specified) of the current time. Return the new time.

setSeconds(sec[,ms])

Set the seconds part and the milliseconds part (if specified) of the current time. Return the new time.

setTime(ms)

Set the date to the specified time in milliseconds. Return the new time.

This function is not generic. If the this object is not a date object then the time is not set and zero will be returned.

setYear(year[, month[, date]])

The same as setFullYear().

setUTCDate (date)

The same as setDate().

setUTCFullYear (year[, month[, date]])

The same as setFullYear().

setUTCHours (hour[, min[, sec[, ms]]])

The same as setHours().

setUTCMilliseconds (ms)

The same as setMilliseconds().

setUTCMinutes (min[, sec[, ms]])

The same as setMinutes).

setUTCMonth (month[, date])

The same as setMonth().

setUTCSeconds(sec[, ms])

The same as setSeconds().

toDateString()

Returns a string primitive. The contents of the string is intended to represent the "date" portion of the Date in the current time zone in a convenient, human-readable form. The string is formatted using the server JVM locale.

toGMTString()

Returns the date converted to a string, using the Internet GMT conventions.

toLocaleDateString([null[, language[, country]]])

The same as toDateString but language and country are used to define the locale used to format the string.

Note: The first argument must be null as this is reserved for future use.

toLocaleString([null[, language[, country]]])

The same as toString but language and country are used to define the locale used to format the string.

Note: The first argument must be null as this is reserved for future use.

toLocaleTimeString([null[, language[, country]]])

The same as toTimeString but language and country are used to define the locale used to format the string.

Note: The first argument must be null as this is reserved for future use.

toInternetTime()

Returns the date converted to a string, using the format "dayOfWeek, dd-MMM-yyyy hh:mm:ss GMT".

toTimeString()

Returns a string value. The contents of the string is intended to represent the "time" portion of the Date in the current time zone in a convenient, human-readable form. The string is formatted using the server JVM locale.

toString()

Returns a string primitive. The contents of the string are intended to represent the Date in the current time zone in a convenient, human-readable form. The string is formatted using the server JVM locale.

toUTCString ( )

The same as toString.

UTC(year, month[, date[, hours[, minutes[, seconds[, ms]]]]])

DEPRECATED. This function has been moved to the Date Constructor object. It is still available from the Date Prototype for backward compatability but may be removed in later releases.

valueOf()

Returns a number, which is this time value.

This function is not generic. If the this object is not a date object then this object is returned.

An Example of Using the Date Object

Shown below is an example of creating a new date object for todays date and then displaying what some of the 'get' functions return.

var today = new Date();
write(“today = “+today+'br');
write(“getDate = “+today.getDate()+'br');
write(“getDay = “+today.getDay()+'br');
write(“getHours = “+today.getHours()+'br');
write(“getMinutes = “+today.getMinutes()+'br');
write(“getSeconds = “+today.getSeconds()+'br');
write(“getTime = “+today.getTime()+'br');
write(“getYear = “+today.getYear()+'br');

This produces the following output:

today = 20 June 2000 13:18
getDate = 20
getDay = 2
getHours = 13
getMinutes = 18
getSeconds = 37
getTime = 961503517966
getYear = 2000

Comment on this topic

Topic ID: 150047