[Prev: Color] [Home] [Next: Font]
Instances of the Date class are used to store and maniupulate dates and times.
A variety of get functions are provided to obtain the date, time or relevant parts, for example, getDate(), getDay(), getFullYear(), getHours(), getMilliseconds(), getMinutes(), getMonth(), getSeconds(), getTime(), getTimezoneOffset(), getUTCDate(), getUTCDay(), getUTCFullYear(), getUTCHours(), getUTCMilliseconds(), getUTCMinutes(), getUTCMonth(), getUTCSeconds() and UTC().
A complementary set of functions are also provided, including setDate(), setFullYear(), setHours(), setMilliseconds(), setMinutes(), setMonth(), setSeconds(), setTime(), setUTCDate(), setUTCFullYear(), setUTCHours(), setUTCMilliseconds(), setUTCMinutes(), setUTCMonth() and setUTCSeconds().
The functions operate using local time, unless they have UTC in their name in which case they use UTC (Universal Coordinated Time, also known as GMT, Greenwich Mean Time).
Conversion between Date objects to and from strings are provided by parse(), toString(), toLocaleString() and toUTCString().
Elapsed time (in milliseconds) can be obtained by subtracting one date from another.
Date() Date( milliseconds ) Date( year, month, day, optHour, optMinutes, optSeconds )
var today = new Date(); var d = new Date( 1234567 ); var date = new Date( 1994, 4, 21 ); var moment = new Date( 1968, 5, 11, 23, 55, 30 ); var gmt = new Date( Date.UTC( 1975, 12, 25, 22, 30 ) );
Dates can be constructed with no arguments, in which case the value is the date and time at the moment of construction using local time. Use the static UTC() function to create a date using UTC time. A single integer argument is taken as the number of milliseconds since midnight on the 1st January 1970.
getDate()
var d = new Date( 1975, 12, 25 ); var x = d.getDate(); // x == 25
Returns the day of the month using local time. The value is always in the range 1..31.
getDate()
var d = new Date( 1975, 12, 25, 22, 30, 15 ); var x = d.getDay(); // x == 0
Returns the day of the week using local time. The value is always in the range 0..6, with the week considered to begin on Sunday.
Example:
var IndexToDay = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]; var d = new Date( 1975, 12, 28 ); System.println( IndexToDay[ d.getDay() ] ); // Prints "Wed"
getFullYear()
var d = new Date( 1975, 12, 25 ); var x = d.getFullYear(); // x == 1975
Returns the year using local time.
getHours()
var d = new Date( 1975, 12, 25, 22 ); var x = d.getHours(); // x == 22
Returns the hour using local time. The value is always in the range 0..23.
getMilliseconds()
var d = new Date( 1975, 12, 25, 22 ); var x = d.getMilliseconds(); // x == 0
Returns the milliseconds component of the date using local time. The value is always in the range 0..999. In the example, x is 0, because no milliseconds were specified, and the default for unspecified components of the time is 0.
getMinutes()
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getMinutes(); // x == 30
Returns the minutes component of the date using local time. The value is always in the range 0..59.
getMonth()
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getMonth(); // x == 11
Returns the months component of the date using local time. The value is always in the range 0..11.
Example:
var IndexToMonth = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; var d = new Date( 1975, 12, 25 ); System.println( IndexToMonth[ d.getMonth() ] ); // Prints "Dec"
getSeconds()
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getSeconds(); // x == 0
Returns the seconds component of the date using local time. The value is always in the range 0..59. In the example x is 0 because no seconds were specified, and the default for unspecified components of the time is 0.
getTime()
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getTime(); // x == 1.91457e+11
Returns the number of milliseconds since midnight on the 1st January 1970 using local time.
getTimezoneOffset()
var d = new Date(); var x = d.getTimezoneOffset();
Returns the difference in minutes between local time and GMT (Greenwich Mean Time -- also known as UTC, Universal Coordinated Time). This value may be positive or negative, and accounts for daylight savings.
getUTCDate()
var d = new Date( 1975, 12, 25 ); var x = d.getUTCDate(); // x == 25
Returns the day of the month using UTC. The value is always in the range 1..31.
getUTCDate()
var d = new Date( 1975, 12, 25, 22, 30, 15 ); var x = d.getUTCDay(); // x == 0
Returns the day of the week using UTC. The value is always in the range 0..6, with the week considered to begin on Sunday.
Example:
var IndexToDay = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]; var d = new Date( 1975, 12, 28 ); System.println( IndexToDay[ d.getUTCDay() ] ); // Prints "Wed"
getUTCFullYear()
var d = new Date( 1975, 12, 25 ); var x = d.getUTCFullYear(); // x == 1975
Returns the year using UTC.
getUTCHours()
var d = new Date( 1975, 12, 25, 22 ); var x = d.getUTCHours(); // x == 22
Returns the hour using UTC. The value is always in the range 0..23.
getUTCMilliseconds()
var d = new Date( 1975, 12, 25, 22 ); var x = d.getUTCMilliseconds(); // x == 0
Returns the milliseconds component of the date using UTC. The value is always in the range 0..999. In the example x is 0 because no milliseconds were specified, and the default for unspecified components of the time is 0.
getUTCMinutes()
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getUTCMinutes(); // x == 30
Returns the minutes component of the date using UTC. The value is always in the range 0..59.
getUTCMonth()
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getUTCMonth(); // x == 11
Returns the months component of the date using UTC. The value is always in the range 0..11.
Example:
var IndexToMonth = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; var d = new Date( 1975, 12, 25 ); System.println( IndexToMonth[ d.getUTCMonth() ] ); // Prints "Dec"
getUTCSeconds()
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getUTCSeconds(); // x == 0
Returns the seconds component of the date using UTC. The value is always in the range 0..59. In the example x is 0 because no seconds were specified, and the default for unspecified components of the time is 0.
parse( dateString )
var d = new Date( Date.parse( "Sun Jan 25 22:30:00 1976 GMT+0000" ) ); d = Date.parse( "Sun, Jan 25 1976 22:30:00 GMT+0000" ); d = Date.parse( "Sun Jan 25 1976" );
This is a static function that parses a string, dateString, which represents a particular date and time. It returns the number of milliseconds since midnight on the 1st January 1970. The string should use IETF standard date format.
The day may optionally be followed by a comma.
The time element may precede or follow the year, or may be omitted entirely. The time element consists of the hour using two digits, followed by a colon followed by the minutes using two digits. This may optionally be followed by a colon followed by the seconds using two digits.
The time zone offset is specified at the end in the form: "GMT" followed by "+" or "-", followed by the number of hours using two digits, followed by the number of minutes using two digits. If the time zone offset is not specified, local time is assumed.
See also toString().
setDate( dayOfTheMonth )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setDate( 30 ); // d == 1975-12-30 22:30
Sets the day of the month to the specified dayOfTheMonth in local time.
setFullYear( year, optMonth, optDayOfTheMonth )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setFullYear( 1980 ); // d == 1980-12-30 22:30 d.setFullYear( 1980, 11, 2 ); // d == 1980-12-02 22:30
Sets the year to the specified year in local time. If the month, optMonth is specified, it must be in the range 0..11. If the optDayOfTheMonth is specified it must be in the range 1..31.
setHours( hour, optMinutes, optSeconds, optMilliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setHours( 10 ); // d == 1980-12-30 10:30
Sets the hour to the specified hour, which must be in the range 0..23, in local time. The minutes, seconds and milliseconds past the hour (optMinutes, optSeconds and optMilliseconds) may also be specified.
setMilliseconds( milliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setMilliseconds( 998 ); // d == 1980-12-30 10:30:00:998
Sets the milliseconds component of the date to the specified value in local time.
setMinutes( minutes, optSeconds, optMilliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setMinutes( 15 ); // d == 1980-12-30 10:15
Sets the minutes to the specified minutes, which must be in the range 0..59, in local time. The seconds and milliseconds past the minute (optSeconds and optMilliseconds) may also be specified.
setMonth( month, optDayOfTheMonth )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setMonth( 0, 11 ); // d == 1980-01-11 22:30
Sets the month to the specified month, which must be in the range 0..11, in local time. The day of the month (optDayOfTheMonth) may also be specified.
setSeconds( seconds, optMilliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setSeconds( 25 ); // d == 1980-12-30 22:30:25
Sets the seconds to the specified seconds, which must be in the range 0..59, in local time. The milliseconds past the minute (optMilliseconds) may also be specified.
setTime( milliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); var duplicate = new Date(); duplicate.setTime( d.getTime() );
Sets the date and time to the local date and time given in terms of milliseconds since midnight on the 1st January 1970.
setUTCDate( dayOfTheMonth )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setUTCDate( 30 ); // d == 1975-12-30 22:30
Sets the day of the month (dayOfTheMonth) to the specified date in UTC.
setUTCFullYear( year, optMonth, optDayOfTheMonth )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setUTCFullYear( 1980 ); // d == 1980-12-30 22:30 d.setUTCFullYear( 1980, 11, 2 ); // d == 1980-12-02 22:30
Sets the year to the specified year in UTC. If the month is specified (optMonth), it must be in the range 0..11. If the day of the month is specified (optDayOfTheMonth), it must be in the range 1..31.
setUTCHours( hour, optMinutes, optSeconds, optMilliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setUTCHours( 10 ); // d == 1980-12-30 10:30
Sets the hour to the specified hour, which must be in the range 0..23, in UTC. The minutes, seconds and milliseconds past the hour (optMinutes, optSeconds and optMilliseconds) may also be specified.
setUTCMilliseconds( milliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setUTCMilliseconds( 998 ); // d == 1980-12-30 10:30:00:998
Sets the milliseconds component of the date to the specified value in UTC.
setUTCMinutes( minutes, optSeconds, optMilliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setUTCMinutes( 15 ); // d == 1980-12-30 10:15
Sets the minutes to the specified minutes, which must be in the range 0..59, in UTC. The seconds and milliseconds past the minute (optSeconds and optMilliseconds) may also be specified.
setUTCMonth( month, optDayOfTheMonth )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setUTCMonth( 0, 11 ); // d == 1980-01-11 22:30
Sets the month to the specified month, which must be in the range 0..11, in local time. The day of the month (optDayOfTheMonth) may also be specified.
setUTCSeconds( seconds, optMilliseconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setUTCSeconds( 25 ); // d == 1980-12-30 22:30:25
Sets the seconds to the specified seconds, which must be in the range 0..59, in UTC. The milliseconds past the minute (optMilliseconds) may also be specified.
toLocaleString()
var d = new Date( 1975, 12, 25, 22, 30 ); var s = d.toLocaleString(); // s == "Wed Dec 25 22:30:00 1975"
Converts the date into a string using the local date format. Depending on the underlying compiler library and the operating system, the string may have a two digit year. Some local date formats may produce strings like "03/05/65": this would be the 3rd May in Europe and the 5th March in North America.
toString()
var d = new Date( 1975, 12, 25, 22, 30 ); var s = d.toString(); // s == "Wed Dec 25 22:30:00 1975 GMT+0000"
Converts the date into a string. This function produces the same output as toLocaleString().
toUTCString()
var d = new Date( 1975, 12, 25, 22, 30 ); var s = d.toUTCString(); // s == "Wed Dec 25 22:30:00 1975 GMT+0000"
Converts the date into a string using the IETF UTC format.
UTC( year, month, day, optHour, optMinutes, optSeconds, optMilliseconds )
var d = new Date( Date.UTC( 1975, 12, 25, 22, 30 ) );
Returns the number of milliseconds since midnight on the 1st January 1970 using UTC. The year, month and day must be specified. The time components, optHour, optMinutes, optSeconds and optMilliseconds, are optional. If you want to specify minutes you must also specify the hour, since you can only skip time components from the right.
[Prev: Color] [Home] [Next: Font]