Overview of Dates and Time in JavaScript



Creating Dates

The Date object handles dates and times. Before a date object can be used, it must be allocated and initialized. Consider these examples of creating new Date instances:

Create the current date and time.
var now = new Date()

Create with the string "Month Day, Year HOURS:MIN:SECS"
var anniversary = new Date("May 25, 1992 13:30:00")

Create with Year, Month, Day
var anniversary = new Date(1992, 5, 25)

Create with Year, Month, Day, Hour, Minutes, Seconds
var anniversary = new Date(1992, May, 25, 13, 30, 0)

To JavaScript, the beginning of time occured on 1 January 1970. Dates before 1970 are invalid.

Date methods

Recovering date information
getDate() Returns the the day of the month for a Date, e.g., now.getDate().
getDay() Returns the the day of the week for a Date, e.g., now.getDay().
getHour() Returns the the hour associated with a Date object, e.g., now.getHours().
getMinutes() Returns the the minutes associated with a Date object, e.g., now.getMinutes().
getMonth() Returns the the month of a Date, e.g., now.getMonth().
getSeconds() Returns the the seconds associated with a Date object, e.g., now.getSeconds().
getTime() Returns the number of milliseconds between 1 January 1970 00:00:00 and the time of the Date, e.g., now.getTime().
getYear() Returns the year associated with a Date object, e.g., now.getYear().
UTC(year, month, day, [hrs,] [min,] [sec]) Returns the number of milliseconds between 1 January 1970 00:00:00 GMT and the time expressed by the parameters, e.g., now.UTC(1996, 5, 25, 11, 30, 00)
Setting date information
setDate(anInteger) Set a Date's day of the month, e.g., now.setDate(25).
setHours(anInteger) Set a Date's hours, e.g., now.setHours(7).
setMinutes(anInteger) Set a Date's minutes, e.g., now.setMinutes(30).
setMonth(anInteger) Set a Date's month, e.g., now.setMonth(12).
setSeconds(anInteger) Set a Date's seconds, e.g., now.setSeconds(45).
setTime(milliseconds) Set a Date by specifying the number of milliseconds elapsed from 1 January 1970 00:00:00, e.g., now.setTime(826579896000).
setYear(anInteger) Set a Date's year, e.g., now.setYear(1999).
Converting dates to strings
toGMTString() Converts a date to a string, using the Internet GMT conventions, e.g., now.toGMTString().
toLocaleString() Converts a date to a string, using the the current locale's convention, e.g., now.toLocaleString().
Recovering time zone information
getTimezoneOffset() Returns the time zone offset (in minutes) for the current locale. The time zone offset is the difference between local time and Greenwich Mean Time (GMT). This will vary according to daylight savings time, e.g., now.getTimezoneOffset().
Special static methods (these must use "Date", not a variable)
Date.parse( aString) Returns milliseconds offset from 1 January 1970 00:00:00 for a given string, e.g., Date.parse("May 25, 1992"), or Date.parse("Sun, May 25, 1992 11:30:00"), or Date.parse("Sun, May 25, 1992 11:30:00 GMT+0430"), etc.
Copyright ©1996 by Charles River Media