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.
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.")
There are only 365398 days remaining to the start of the
next millennium.
This code uses Date objects to calculate the time remaining, in days, to the start of the next millennium.
Syntax: Object.constructor
This specifies a function to create an object's property and is inherited by all objects from their prototype.
Syntax: Object.prototype.name = value
This allows the addition of properties and methods to any object.
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.
Syntax: object.getDate( )
This method returns an integer (between 1 and 31) representing the day of the month for the specified (local time) date.
Syntax: object.getDay( )
This method returns an integer (0 for Sunday thru 6 for Saturday) representing the day of the week.
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.
Syntax: object.getHours( )
This method returns an integer between 0 and 23 that represents the hour (local time) for the specified date.
Syntax: object.getMilliseconds( )
This method returns an integer between 0 and 999 that represents the milliseconds (local time) for the specified date.
Syntax: object.getMinutes( )
This method returns an integer between 0 and 59 that represents the minutes (local time) for the specified date.
Syntax: object.getMonth( )
This method returns an integer (0 for January thru 11 for December) that represents the month for the specified date.
Syntax: object.getSeconds( )
This method returns an integer between 0 and 59 that represents the seconds (local time) for the specified date.
Syntax: object.getTime( )
This method returns a numeric value representing the number of milliseconds since midnight 01/01/1970 for the specified date.
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.
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.
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.
Syntax: object.getUTCFullYear( )
This method returns a four-digit absolute number that represents the year, according to universal time, for the supplied date.
Syntax: object.getUTCHours( )
This method returns an integer between 0 and 23 that represents the hours, according to universal time, in the supplied date.
Syntax: object.getUTCMilliseconds( )
This method returns an integer between 0 and 999 that represents the milliseconds, according to universal time, in the specified date.
Syntax: object.getUTCMinutes( )
This method returns an integer between 0 and 59 that represents the minutes, in universal time, for the supplied date.
Syntax: object.getUTCMonth
This method returns an integer, 0 for January thru 11 for December, according to universal time, for the specified date.
Syntax: object.getUTCSeconds( )
This method returns an integer between 0 and 59 that represents the seconds, according to universal time, for the specified date.
Syntax: Date.parse(dateString)
This method returns takes a date string and returns the number of milliseconds since January 01 1970 00:00:00.
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.
Syntax: object.setFullYear(yearVal [, monthVal, dayVal])
This method is used to set the full year for the supplied date according to local time.
Syntax: object.setHours(hoursVal [, minutesVal, secondsVal, msVal])
This method is used to set the hours for the supplied date according to local time.
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.
Syntax: object.setMinutes(minutesVal [, secondsVal, msVal])
This method is used to set the minutes for the supplied date according to local time.
Syntax: object.setMonth(monthVal [, dayVal])
This method is used to set the month for the supplied date according to local time.
Syntax: object.setSeconds(secondsVal [, msVal])
This method is used to set the seconds for the specified date according to local time.
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.
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.
Syntax: object.setUTCFullYear(yearVal [, monthVal, dayVal])
This method is used to set the full year for the supplied date according to universal time.
Syntax: object.setUTCHours(hoursVal [, minutesVal, secondsVal, msVal])
This method is used to set the hours for the supplied date according to universal time.
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.
Syntax: object.setUTCMinutes(minutesVal [, secondsVal, msVal])
This method is used to set the minutes for the supplied date according to universal time.
Syntax: object.setUTCMonth(monthVal [, dayVal])
This method is used to set the month for the supplied date according to universal time.
Syntax: object.setUTCSeconds(secondsVal [, msVal])
This method is used to set the seconds for the specified date according to universal time.
Syntax: object.toGMTString( )
This method converts a local date to Greenwich Mean Time.
Syntax: object.toLocaleString( )
This method uses the relevant locale's date conventions when converting a date to a string.
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.
Syntax: Object.toString()
The toString method returns a string representing a specified object.
Syntax: object.toUTCString( )
This method uses the universal time convention when converting a date to a string.
Syntax: Object.unwatch(property)
This method removes a watchpoint set for an object and property name with the watch method.
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().
Syntax: Object.valueOf()
This method returns a primitive value for a specified object.
Syntax: Object.watch(property, handlerfunction)
This method adds a watchpoint to a property of the object.