Javascript 18: The Date Object

Javascript provides three built-in objects with ready-made properties and methods:

  1. The Date Object
  2. The String Object
  3. The Math Object

The base date for the Date object is 1/1/70, a limitation imposed by the need for cross-platform uniformity. PCs and Macs can work with dates before this but UNIX systems cannot and this is the limiting factor.

The syntax for creating a new date is:

  variableName=new Date(parameters)

For example: Today=new Date();

This example retrieves the date from the system clock. The date is made up of the following elements:

DayMonthDayHours:Minutes:SecondsYear

There are many date methods as follows:

MethodExampleReturns
getDatetoday.getDate()5
getDayyesterday.getDay()2
getHoursToday.getHours()5
getMonthyear.getMonth()30
getSecondsTime.getSeconds()6
getTimenow.getTime()13
getTimeZoneoffsettoday.getTimeZoneOffset
getYearnow.getYear97 (years since 1900)
parseDate.parse(July 1, 1997N/A
setDatenow.setDate(6)N/A
setHoursnow.setHours(14)N/A
setMinutesnow.setMinutes(50)N/A
setMonthtoday.setMonth(7)N/A
setSecondstoday.setSeconds(30)N/A
setTimetoday.setTime(yesterday.getTime())N/A
setYeartoday.setYear(97)N/A
toGMTStringyesterday.toGMTString()Sat, Feb 24 1996 14:28:15 GMT
toLocaleStringtoday.toLocaleString()2/25/96 14:28:15
UTC96,11,3,0,0,0N/A

The GET methods return an integer representing the part of the date requested. The SET methods set the part of the date to the value of the parameter supplied. PARSE methods interpret date strings and the single TO method converts a date to a GMT value.

Date numbers are all zero-based as follows:

Date AttributeNumeric range
seconds, minutes0-59
hours0-23
day (0=Sunday)0-6
month (0=January0-11
year0 + number of years since 1900



Return to Javascript Menu