Date Object

JavaScript does not have a date data type. However, the date object and its methods enable you to work with dates and times in your applications. The date object has a large number of methods for getting, setting, and manipulating dates. It does not have any properties.

JavaScript handles dates very similarly to Java. The two languages have many of the same date methods, and both languages store dates as the number of milliseconds (1000 milliseconds equals 1 second) since January 1, 1970 00:00:00.

To create a date object:

var varName = new Date(parameters)

where varName is a JavaScript variable name for the date object being created. Since we cannot use the "real" Date Object we use the key new to create a new instance of the Date Object. The Date Object is used as a template to create instances of the Date Object which we can use.

The parameters for the Date constructor can be any of the following:

The Date object has a large number of Methods for handling dates and times. The Methods fall into these broad categories:

  • "get" methods, for getting date and time values from date objects
  • "set" methods, for setting date and time values in date objects
  • "to" methods, for returning string values from date objects.
  • "parse" and "UTC" methods, for parsing date strings.

The "get" and "set" methods enable you to get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay() Method that returns the day of the week, but no corresponding setDay() Method, because the day of the week is set automatically. These methods use integers to represent these values as follows:

  • seconds and minutes: 0 to 59
  • hours: 0 to 23
  • day: 0 to 6 (day of the week with 0 = Sunday, 1 = Monday, and so on)
  • date: 1 to 31 (day of the month)
  • months: 0 (January) to 11 (December)
  • year: years since 1900

For example, suppose you define the following date:

var Xmas95 = new Date("December 25, 1995");

Then Xmas95.getMonth() returns 11, and Xmas95.getYear() returns 95.

The getTime and setTime methods are useful for comparing dates. The getTime method returns the number of milliseconds since the epoch for a Date Object as mentioned above.

For example, the following code displays the number of shopping days left until Christmas:

var now = new Date();
var thisYear = now.getFullYear();

var Xmas = "December 25, " + thisYear;

var nextXmas = new Date(Xmas);

var msPerDay = 24 * 60 * 60 * 1000 ;  // Number of milliseconds per day

var daysLeft = (nextXmas.getTime() - now.getTime()) / msPerDay;
daysLeft = Math.round(daysLeft);

alert("Number of Shopping Days until Christmas: " + daysLeft);

This example creates a Date Object named now that contains today's date. It then creates a Date Object named nextXmas, and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and nextXmas, using getTime(), and rounding to a whole number of days.

The parse() Method is useful for assigning values from date strings to existing Date Objects. For example, the following code uses parse() and setTime() to assign a date to the IPOdate object.

var IPOdate = new Date();
IPOdate.setTime(Date.parse("Aug 9, 2001"));

Dealing with dates is one of the most tedious tasks in any language. This is because many people like to represent dates and times in decidedly non-decimal systems. Months come in units of 12, hours in units of 24, and minutes and seconds in units of 60. All these variations are quite illogical from the computer's standpoint. It likes to deal with nice, round numbers, preferably powers of 2, or at least multiples of 10.

The Date Object has no Properties, but many Methods. In order to use the Date Object you must first understand how to construct instances of it. There are three basic methods of creating a Date instance, as follows:

  1. new Date() - constructs a Date instance that represents the current date and time. This should be accurate to within a second, and also include information about your time zone and any corrections to it currently in effect (such as Daylight Savings Time).
  2. new Date(datestring) - takes a string of the form "Month Day, Year" such as "November 23, 1990" and converts it to a Date instance. This string may optionally have a time of the form HH:MM:SS at the end, which is used to set the time to HH hours, MM minutes, and SS seconds. Hours should be specified using a 24-hour clock, also known as military time, so that 10:15 PM is represented as 22:15:00.
  3. new Date(yr, mon, day) - takes three integers representing the year, month, and day.

NOTE: that the month is always indexed from zero, so that November is month 10.

The year can also be offset by 1900, so that you can use either of these two forms

var NovDate = new Date(90, 10, 23);
var NovDate = new Date(1990, 10, 23);

to create a Date instance named NovDate for November 23, 1990. NOTE that for the year 2000 and beyond you must use the second form. This form may optionally take an additional three integer arguments for the time, so that 1:05 PM on November 23, 1990 is

var NovDate2 = new Date(90, 10, 23, 13, 5, 0);

The Date object has a large set of methods for getting and setting the components of a date. These methods are as follows:

var now = new Date();  // now is Sat Mar 24 23:30:10 PST 2001

Date Object Methods

METHOD EXAMPLES RETURNED VALUE
now.getDate() 24
now.getDay() 6 (Saturday)
now.getHours() 23
now.getMinutes() 30
now.getMonth() 2 (March)
now.getSeconds() 10
now.getTime() 985505410156 - the internal, millisecond representation of a Date Object
now.getTimezoneOffset() 480 - Time Zone Difference, in minutes, between this Date and GMT
now.getYear() 2001
99 (the years since 1900 NOTE: before 2000)
now.getFullYear() 2001 - is a full four-digit year including the century, not a two-digit abbreviation as with getYear()
someDate.parse(July 1, 1976) converts a string representation of a date to the internal milliseconds representation
someDate.setDate(6) -
someDate.setHours(14) -
someDate.setMinutes(50) -
someDate.setMonth(7) -
someDate.setSeconds(7) -
someDate.setTime() -
someDate.setYear(88) -
now.toGMTString() Sun, 25 Mar 2001 07:30:10 UTC
now.toLocaleString() Saturday, March 24, 2001 11:30:10 PM
someDate.UTC(99, 11, 3, 0, 0, 0) -

NOTE: Date.parse() and Date.UTC() since they are static; they may not be used with Date instances. Since they return the internal representation of dates, these values are often simply passed to setTime().