Date object constructorS2C Home « Globals « Date

In JavaScript you create a date instance using the constructor Date.

Description

Date instances allow us to store date and time data which we can manipulate.

  • When no arguments are passed a new Date instance is created with locale specific current date and time.
  • When passing a string for the dateString argument the format must be compatible with the parse method.
  • When passing the integer for the milliseconds this represents the number of milliseconds since 1 January 1970 00:00:00 UTC (Universal Time Coordinated).
    • When passing a negative integer dates will be calculated prior to 1 January 1900 00:00:00 UTC.
  • When using any other arguments you must supply at least the year, month, and day. Other arguments that are omitted have their value set to 0.

Syntax

Signature Description
var aDate = new Date();Create a Date object.
var aDate = new Date(dateString);Create a Date object using the specified dateString.
var aDate = new Date(milliseconds);Create a Date object using the specified number of milliseconds.
var aDate = new Date(year, month, day
    [, hour, minute, second, millisecond ]);
Create a Date object using the specified year, month and day optionally passing the hour, minute, second and millisecond.

Parameters

Parameter Description
dateStringA string representation of a date.
millisecondsAn integer.
yearAn integer for the year (2011 for example).
monthAn integer in the range 0-11 (0 for January).
dayAn integer in the range 1-31.
hourAn integer in the range 0-23.
minuteAn integer in the range 0-59.
secondAn integer in the range 0-59.
millisecondAn integer in the range 0-999.

Class Properties

Class Property Description
prototypeEnables property assignment to objects of type Date.

Instance Properties

Instance Property Description
constructorReturns a reference to the Date function that created the prototype.

Class Methods

Class Method Description
now()Returns an integer value representing the milliseconds elapsed since 1 January 1970 00:00:00 UTC.
parse()Parses a string representation of a date and returns the number of milliseconds since 1 January 1970 00:00:00 UTC.
UTC()Accepts a user input date and returns an integer value representing the milliseconds elapsed time since 1 January 1970 00:00:00 UTC.

Instance Methods

Instance Method Description
Getter (Accessor) MethodsThese methods DO NOT modify the Date object
getDate()Returns the locale specific day of the month (1-31) for the specified date.
getDay()Returns the locale specific day of the week (0-6) for the specified date.
getFullYear()Returns the locale specific year (CCYY) for the specified date.
getHours()Returns the locale specific hour of the day (0-23) for the specified date.
getMilliseconds()Returns the locale specific milliseconds (0-999) for the specified date.
getMinutes()Returns the locale specific minute of the hour (0-59) for the specified date.
getMonth()Returns the locale specific month of the year (0-11) for the specified date.
getSeconds()Returns the locale specific second of the minute (0-59) for the specified date.
getTime()Returns the number of milliseconds since 1 January 1970 00:00:00 UTC.
getTimezoneOffset()Returns the locale specific time zone offset from UTC in minutes.
getUTCDate()Returns the UTC day of the month (1-31) for the specified date.
getUTCDay()Returns the UTC day of the week (0-6) for the specified date.
getUTCFullYear()Returns the UTC year (CCYY) for the specified date.
getUTCHours()Returns the UTC hour of the day (0-23) for the specified date.
getUTCMilliseconds()Returns the UTC milliseconds (0-999) for the specified date.
getUTCMinutes()Returns the UTC minute of the hour (0-59) for the specified date.
getUTCMonth()Returns the UTC month of the year (0-11) for the specified date.
getUTCSeconds()Returns the UTC second of the minute (0-59) for the specified date.
toDateString()Returns a string of the date portion of the specified date.
toLocaleDateString()Returns a locale specific string of the date portion of the specified date.
toLocaleString()Returns a locale specific string of the specified date.
toLocaletimeString()Returns a locale specific string of the time portion of the specified date.
toString()Returns a locale specific string representation of the specified date.
toTimeString()Returns a string of the time portion of the specified date.
toUTCString()Returns a UTC specific string of the specified date.
valueOf()Returns the primitive value of a Date object.
Setter (Mutator) MethodsThese methods DO modify the Date object
setDate()Sets the locale specific day of the month (1-31) for the specified date
setFullYear()Sets the locale specific year for the specified date.
setHours()Sets the locale specific hour of the day (0-23) for the specified date.
setMilliseconds()Sets the locale specific milliseconds (0-999) for the specified date.
setMinutes()Sets the locale specific minute of the hour (0-59) for the specified date.
setMonth()Sets the locale specific month of the year (0-11) for the specified date.
setSeconds()Sets the locale specific second of the minute (0-59) for the specified date.
setTime()Sets the locale specific number of milliseconds since 1 January 1970 00:00:00 UTC.
setUTCDate()Sets the UTC day of the month (1-31) for the specified date.
setUTCFullYear()Sets the UTC year (CCYY) for the specified date.
setUTCHours()Sets the UTC specific hour of the day (0-23) for the specified date.
setUTCMilliseconds()Sets the UTC milliseconds (0-999) for the specified date.
setUTCMinutes()Sets the UTC minute of the hour (0-59) for the specified date.
setUTCMonth()Sets the UTC month of the year (0-11) for the specified date.
setUTCSeconds()Sets the UTC second of the minute (0-59) for the specified date.

Creating Dates

Dates can be created using the syntax described above. When used without the new special operator returns a string representing the current date.

Examples



// Create a Date instance for the current date and time.
var aDate = new Date();
alert(aDate);

// Create a Date instance using the milliseconds parameter.
// Since 1 January 1970 00:00:00 UTC (Universal Time Coordinated).
var bDate = new Date(300000000);
alert(bDate);

// Create a negative Date instance using the milliseconds parameter.
// Before 1 January 1970 00:00:00 UTC (Universal Time Coordinated).
var cDate = new Date(-300000000);
alert(cDate);

// Parse a Date.
var dDate = new Date('Jan 1, 2000');
alert(dDate);

Press the button below to action the above code:


Related Tutorials

JavaScript Intermediate Tutorials - Lesson 2 - Dates and Times

go to home page Homepage go to top of page Top