Date Type

The JavaScript Date type stores dates as the number of milliseconds since midnight on January 1, 1970 UTC (Universal Time Code).

To create a date object:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var aDate = new Date(); 
        document.writeln(aDate)
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The created object aDate is assigned the current date and time.

To create a date with the number of milliseconds after midnight, January 1, 1970 UTC.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var aDate = new Date(1111111); 
        document.writeln(aDate);//Wed Dec 31 1969 16:18:31 GMT-0800 (Pacific Standard Time)
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

We can pass in a string value representing the date value.

The following date formats are supported:

FormatExample
month/date/year 7/24/2012
month_name date, yearJanuary 31, 2021
day_of_week month_name date year hours:minutes:seconds time_zoneTue May 27 2012 12:34:56 GMT-0400
ISO 8601 extended format YYYY-MM-DDTHH:mm:ss.sssZ2012-0525T00:00:00
 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">

        var aDate = new Date("May 25, 2004"); 
        document.writeln(aDate);//Tue May 25 2004 00:00:00 GMT-0700 (Pacific Daylight Time)
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Or we can pass in numbers:

  • the year,
  • the zero-based month (January is 0, February is 1, and so on)
  • the day of the month (1 through 31),
  • the hours (0 through 23),
  • minutes,
  • seconds,
  • milliseconds

The year and month are required.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var aDate = new Date(2012, 4, 5, 17, 55, 55); 
        document.writeln(aDate);//Sat May 05 2012 17:55:55 GMT-0700 (Pacific Daylight Time)
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

If the day of the month isn't supplied, it's assumed to be 1. all other omitted arguments are assumed to be 0.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var aDate = new Date(2012, 0); 
        document.writeln(aDate);//Sun Jan 01 2012 00:00:00 GMT-0800 (Pacific Standard Time)
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The following table lists the methods of Date object

getDate()
Returns the day of the month (1 through 31) for the date.
getDay()
Returns the date's day of the week as a number (where 0 represents Sunday and 6 represents Saturday).
getFullYear()
Returns the four-digit year (2007 instead of just 07).
getHours()
Returns the date's hours as a number between 0 and 23.
getMilliseconds()
Returns the date's milliseconds.
getMinutes()
Returns the date's minutes as a number between 0 and 59.
getMonth()
Returns the month of the date, where 0 represents January and 11 represents December.
getSeconds()
Returns the date's seconds as a number between 0 and 59.
getTime()
Returns the milliseconds representation of the date; same as valueOf().
getTimezoneOffset()
Returns the number of minutes that the local time zone is off set from UTC. For example, Eastern Standard Time returns 300. This value changes when an area goes into Daylight Saving Time.
getUTCDate()
Returns the day of the month (1 through 31) for the UTC date.
getUTCDay()
Returns the UTC date's day of the week as a number (where 0 represents Sunday and 6 represents Saturday).
getUTCFullYear()
Returns the four-digit year of the UTC date value.
getUTCHours()
Returns the UTC date's hours as a number between 0 and 23.
getUTCMilliseconds()
Returns the UTC date's milliseconds.
getUTCMinutes()
Returns the UTC date's minutes as a number between 0 and 59.
getUTCMonth()
Returns the month of the UTC date, where 0 represents January and 11 represents December.
getUTCSeconds()
Returns the UTC date's seconds as a number between 0 and 59.
now()
returns current time in the millisecond.
parse()
accepts a string argument representing a date.
setDate(date)
Sets the day of the month for the date. If the date is greater than the number of days in the month, the month value also gets increased.
setFullYear(year)
Sets the year of the date. The year must be given with four digits (2007 instead of just 07).
setHours(hours)
Sets the date's hours. Setting the hours to a number greater than 23 also increments the day of the month.
setMilliseconds
Sets the date's milliseconds. (milliseconds)
setMinutes(minutes)
Sets the date's minutes. Setting the minutes to a number greater than 59 also increments the hour.
setMonth(month)
Sets the month of the date, which is any number 0 or greater. Numbers greater than 11 add years.
setSeconds(seconds)
Sets the date's seconds. Setting the seconds to a number greater than 59 also increments the minutes.
setTime(milliseconds)
Sets the milliseconds representation of the date, thus changing the entire date.
setUTCDate(date)
Sets the day of the month for the UTC date. If the date is greater than the number of days in the month, the month value also gets increased.
setUTCFullYear(year)
Sets the year of the UTC date. The year must be given with four digits (2007 instead of just 07).
setUTCHours(hours)
Sets the UTC date's hours. Setting the hours to a number greater than 23 also increments the day of the month.
setUTCMilliseconds
Sets the UTC date's milliseconds. (milliseconds)
setUTCMinutes(minutes)
Sets the UTC date's minutes. Setting the minutes to a number greater than 59 also increments the hour.
setUTCMonth(month)
Sets the month of the UTC date, which is any number 0 or greater. Numbers greater than 11 add years.
setUTCSeconds(seconds)
Sets the UTC date's seconds. Setting the seconds to a number greater than 59 also increments the minutes.
toDateString()
displays the date's day of the week, month, day of the month, and year.
toLocaleDateString()
displays the date's day of the week, month, day of the month, and year.
toLocaleString()
returns the date and time in a locale format.
toLocaleTimeString()
displays the date's hours, minutes, and seconds.
toString()
returns the date and time with time-zone information.
toTimeString()
displays the date's hours, minutes, seconds, and time zone.
toUTCString()
displays the complete UTC date.
UTC()
returns the millisecond of a date.
valueOf()
returns the milliseconds representation of the date.
Home 
  JavaScript Book 
    Essential Types  

Date:
  1. Date Type
  2. Date getter
  3. Date Setter
  4. Date.parse()
  5. Date.UTC()
  6. Date.now()
  7. Date toLocaleString()
  8. Date toString()
  9. Date valueOf()
  10. Date toDateString()
  11. Date toTimeString()
  12. Date toLocaleDateString()
  13. Date toLocaleTimeString()
  14. Date toUTCString()