JavaScript » Objects » Date

Syntax:
new Date()

new Date(milliseconds)

new Date(dateString)

new Date(yr_num, mo_num, day_num [, hr_num, min_num,
                sec_num, ms_num])
milliseconds
an integer that represents the number of milliseconds since 01/01/70 00:00:00.
dateString
a string that represents the date in a format that is recognized by the Date.parse method.
yr_num, mo_num, day_num
an integer that represents the year, month or day of the date.
hr_num, min_num, sec_num, ms_num
an integer that represents the hours, minutes, seconds and milliseconds.

The Date object allows you to work programatically with dates and times. You create a Date object using the Date constructor as shown in the syntax.

If you don't supply any of the above parameters, JavaScript creates an object for today's date according to the time on the local machine. If any arguments are supplied, you have to include the year, month and day as a minimum, with the time parameters being optional. Note that if you only supply some arguments, any not supplied are set to 0.

All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds. The range of a Date object relative to 01/01/1970 (UTC) is -100,000,000 to 100,000,000 days and both Universal (UTC) time and Greenwich Mean Time (GMT) are supported.

Examples

Code:
d = new Date()    //today's date
mill=new Date(3001, 00, 01, 00, 00, 00)    //Next millennium start date
diff = mill-d    //difference in milliseconds
mtg = new String(diff/86400000)    //calculate days and convert to string
point=mtg.indexOf(".")    //find the decimal point
days=mtg.substring(0,point)    //get just the whole days
document.write("There are only " + days + " days remaining to the start of the next millennium.")
Output:
There are only 365398 days remaining to the start of the next millennium.
Explanation:

This code uses Date objects to calculate the time remaining, in days, to the start of the next millennium.

Properties

constructor

Syntax: Object.constructor

This specifies a function to create an object's property and is inherited by all objects from their prototype.

prototype

Syntax: Object.prototype.name = value

This allows the addition of properties and methods to any object.

Methods

eval

Syntax: Object.eval(string)

The eval method is deprecated as a method of Object, but is still used as a high level function. It evaluates a string of JavaScript in the context of an object.

getDate

Syntax: object.getDate( )

This method returns an integer (between 1 and 31) representing the day of the month for the specified (local time) date.

getDay

Syntax: object.getDay( )

This method returns an integer (0 for Sunday thru 6 for Saturday) representing the day of the week.

getFullYear

Syntax: object.getFullYear( )

This method returns an integer representing the year of a specified date. The integer returned is a four digit number, 1999, for example, and this method is to be prefered over getYear.

getHours

Syntax: object.getHours( )

This method returns an integer between 0 and 23 that represents the hour (local time) for the specified date.

getMilliseconds

Syntax: object.getMilliseconds( )

This method returns an integer between 0 and 999 that represents the milliseconds (local time) for the specified date.

getMinutes

Syntax: object.getMinutes( )

This method returns an integer between 0 and 59 that represents the minutes (local time) for the specified date.

getMonth

Syntax: object.getMonth( )

This method returns an integer (0 for January thru 11 for December) that represents the month for the specified date.

getSeconds

Syntax: object.getSeconds( )

This method returns an integer between 0 and 59 that represents the seconds (local time) for the specified date.

getTime

Syntax: object.getTime( )

This method returns a numeric value representing the number of milliseconds since midnight 01/01/1970 for the specified date.

getTimezoneOffset

Syntax: object.getTimezoneOffset( )

This method returns the difference in minutes between local time and Greenwich Mean Time. This value is not a constant, as you might think, because of the practice of using Daylight Saving Time.

getUTCDate

Syntax: object.getUTCDate( )

This method returns an integer between 1 and 31 that represents the day of the month, according to universal time, for the specified date.

getUTCDay

Syntax: object.getUTCDay( )

This method returns an integer (0 for Sunday thru 6 for Saturday) that represents the day of the week, according to universal time, for the specified date.

getUTCFullYear

Syntax: object.getUTCFullYear( )

This method returns a four-digit absolute number that represents the year, according to universal time, for the supplied date.

getUTCHours

Syntax: object.getUTCHours( )

This method returns an integer between 0 and 23 that represents the hours, according to universal time, in the supplied date.

getUTCMilliseconds

Syntax: object.getUTCMilliseconds( )

This method returns an integer between 0 and 999 that represents the milliseconds, according to universal time, in the specified date.

getUTCMinutes

Syntax: object.getUTCMinutes( )

This method returns an integer between 0 and 59 that represents the minutes, in universal time, for the supplied date.

getUTCMonth

Syntax: object.getUTCMonth

This method returns an integer, 0 for January thru 11 for December, according to universal time, for the specified date.

getUTCSeconds

Syntax: object.getUTCSeconds( )

This method returns an integer between 0 and 59 that represents the seconds, according to universal time, for the specified date.

parse

Syntax: Date.parse(dateString)

This method returns takes a date string and returns the number of milliseconds since January 01 1970 00:00:00.

setDate

Syntax: object.setDate(dateVal)

This method is used to set the day of the month, using an integer from 1 to 31, for the supplied date according to local time.

setFullYear

Syntax: object.setFullYear(yearVal [, monthVal, dayVal])

This method is used to set the full year for the supplied date according to local time.

setHours

Syntax: object.setHours(hoursVal [, minutesVal, secondsVal, msVal])

This method is used to set the hours for the supplied date according to local time.

setMilliseconds

Syntax: object.setMilliseconds(millisecondsVal)

This method is used to set the milliseconds for the supplied date according to local time. The millisecondsVal parameter expects a number between 0 and 999 athough if this is exceeded, the setMilliseconds method will automatically increment other values in the Date object, e.g. if 1020 is specified, the seconds value is incremented by one and millisecondsVal is set to 20.

setMinutes

Syntax: object.setMinutes(minutesVal [, secondsVal, msVal])

This method is used to set the minutes for the supplied date according to local time.

setMonth

Syntax: object.setMonth(monthVal [, dayVal])

This method is used to set the month for the supplied date according to local time.

setSeconds

Syntax: object.setSeconds(secondsVal [, msVal])

This method is used to set the seconds for the specified date according to local time.

setTime

Syntax: object.setTime(timeVal)

This method is used to set the time of a Date object according to local time. The timeVal argument is an integer that represents the number of milliseconds elapsed since 1 January 1970 00:00:00.

setUTCDate

Syntax: object.setUTCDate(dateVal)

This method is used to set the day of the month, using an integer from 1 to 31, for the supplied date according to universal time.

setUTCFullYear

Syntax: object.setUTCFullYear(yearVal [, monthVal, dayVal])

This method is used to set the full year for the supplied date according to universal time.

setUTCHours

Syntax: object.setUTCHours(hoursVal [, minutesVal, secondsVal, msVal])

This method is used to set the hours for the supplied date according to universal time.

setUTCMilliseconds

Syntax: object.setUTCMilliseconds(millisecondsVal)

This method is used to set the milliseconds for the supplied date according to universal time. The millisecondsVal parameter expects a number between 0 and 999 athough if this is exceeded, the setMilliseconds method will automatically increment other values in the Date object, e.g. if 1020 is specified, the seconds value is incremented by one and millisecondsVal is set to 20.

setUTCMinutes

Syntax: object.setUTCMinutes(minutesVal [, secondsVal, msVal])

This method is used to set the minutes for the supplied date according to universal time.

setUTCMonth

Syntax: object.setUTCMonth(monthVal [, dayVal])

This method is used to set the month for the supplied date according to universal time.

setUTCSeconds

Syntax: object.setUTCSeconds(secondsVal [, msVal])

This method is used to set the seconds for the specified date according to universal time.

toGMTString

Syntax: object.toGMTString( )

This method converts a local date to Greenwich Mean Time.

toLocaleString

Syntax: object.toLocaleString( )

This method uses the relevant locale's date conventions when converting a date to a string.

toSource

Syntax: Object.toSource()

The toSource method returns a literal representing the source code of an object. This can then be used to create a new object.

toString

Syntax: Object.toString()

The toString method returns a string representing a specified object.

toUTCString

Syntax: object.toUTCString( )

This method uses the universal time convention when converting a date to a string.

unwatch

Syntax: Object.unwatch(property)

This method removes a watchpoint set for an object and property name with the watch method.

UTC

Syntax: Date.UTC(year, month, day [, hours, minutes, seconds, ms])

This method returns the number of milliseconds from the date in a Date object since January 1, 1970 00:00:00 according to universal time. This is a static method of Date so the format is always Date.UTC() as opposed to objectName.UTC().

valueOf

Syntax: Object.valueOf()

This method returns a primitive value for a specified object.

watch

Syntax: Object.watch(property, handlerfunction)

This method adds a watchpoint to a property of the object.

See Also: