JavaScript Date Objects

By | August 24, 2022

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

Wed Aug 24 2022 09:50:25 GMT-0700 (Pacific Daylight Time)

JavaScript Date Output

By default, JavaScript will use the browser’s time zone and display a date as a full text string:

Wed Aug 24 2022 09:41:54 GMT-0700 (Pacific Daylight Time)

You will learn much more about how to display dates, later in this tutorial.

Creating Date Objects

Date objects are created with the new Date() constructor.

There are 4 ways to create a new date object:new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)

new Date()

new Date() creates a new date object with the current date and time:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(), creates a new date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

Using new Date(), creates a new date object with the current date and time:

Wed Aug 24 2022 09:51:22 GMT-0700 (Pacific Daylight Time)

Date objects are static. The computer time is ticking, but date objects are not.

new Date(year, month, …)

new Date(year, month, ...) creates a new date object with a specified date and time.

7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(7 numbers), creates a new date object with the specified date and time:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10, 33, 30, 0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

Using new Date(7 numbers), creates a new date object with the specified date and time:

Mon Dec 24 2018 10:33:30 GMT-0800 (Pacific Standard Time)

Note: JavaScript counts months from 0 to 11:

January = 0.

December = 11.

Specifying a month higher than 11, will not result in an error but add the overflow to the next year:

Specifying:const d = new Date(2018, 15, 24, 10, 33, 30);

Is the same as:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>JavaScript counts months from 0 to 11.</p>
<p>Specifying a month higher than 11, will not result in an error but add the overflow to the next year:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 15, 24, 10, 33, 30, 0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

JavaScript counts months from 0 to 11.

Specifying a month higher than 11, will not result in an error but add the overflow to the next year:

Wed Apr 24 2019 10:33:30 GMT-0700 (Pacific Daylight Time)

Specifying a day higher than max, will not result in an error but add the overflow to the next month:

Specifying:const d = new Date(2018, 5, 35, 10, 33, 30);

Is the same as:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>JavaScript counts months from 0 to 11.</p>
<p>Specifying a day higher than max, will not result in an error but add the overflow to the next month:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 05, 35, 10, 33, 30, 0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

JavaScript counts months from 0 to 11.

Specifying a day higher than max, will not result in an error but add the overflow to the next month:

Thu Jul 05 2018 10:33:30 GMT-0700 (Pacific Daylight Time)

Using 6, 4, 3, or 2 Numbers

6 numbers specify year, month, day, hour, minute, second:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>6 numbers specify year, month, day, hour, minute and second:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10, 33, 30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

6 numbers specify year, month, day, hour, minute and second:

Mon Dec 24 2018 10:33:30 GMT-0800 (Pacific Standard Time)

5 numbers specify year, month, day, hour, and minute:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>5 numbers specify year, month, day, hour, and minute:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10, 33);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

5 numbers specify year, month, day, hour, and minute:

Mon Dec 24 2018 10:33:00 GMT-0800 (Pacific Standard Time)

4 numbers specify year, month, day, and hour:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>4 numbers specify year, month, day, and hour:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

4 numbers specify year, month, day, and hour:

Mon Dec 24 2018 10:00:00 GMT-0800 (Pacific Standard Time)

3 numbers specify year, month, and day:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>3 numbers specify year, month, and day:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

3 numbers specify year, month, and day:

Mon Dec 24 2018 00:00:00 GMT-0800 (Pacific Standard Time)

2 numbers specify year and month:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>2 numbers specify year and  month:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

2 numbers specify year and month:

Sat Dec 01 2018 00:00:00 GMT-0800 (Pacific Standard Time)

You cannot omit month. If you supply only one parameter it will be treated as milliseconds.

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>One parameter will be interpreted as new Date(milliseconds).</p>
<p id="demo"></p>
<script>
const d = new Date(2018);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

One parameter will be interpreted as new Date(milliseconds).

Wed Dec 31 1969 16:00:02 GMT-0800 (Pacific Standard Time)

Previous Century

One and two digit years will be interpreted as 19xx:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Two digit years will be interpreted as 19xx:</p>
<p id="demo"></p>
<script>
const d = new Date(99, 11, 24);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

Two digit years will be interpreted as 19xx:

Fri Dec 24 1999 00:00:00 GMT-0800 (Pacific Standard Time)

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>One digit years will be interpreted as 19xx:</p>
<p id="demo"></p>
<script>
const d = new Date(9, 11, 24);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

One digit years will be interpreted as 19xx:

Fri Dec 24 1909 00:00:00 GMT-0800 (Pacific Standard Time)

new Date(dateString)

new Date(dateString) creates a new date object from a date string:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>A Date object can be created with a specified date and time:</p>
<p id="demo"></p>
<script>
const d = new Date("October 13, 2014 11:13:00");
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

A Date object can be created with a specified date and time:

Mon Oct 13 2014 11:13:00 GMT-0700 (Pacific Daylight Time)

Date strings are described in the next chapter.

JavaScript Stores Dates as Milliseconds

JavaScript stores dates as number of milliseconds since January 01, 1970, 00:00:00 UTC (Universal Time Coordinated).

Zero time is January 01, 1970 00:00:00 UTC.

Now the time is: 1661359314266 milliseconds past January 01, 1970

new Date(milliseconds)

new Date(milliseconds) creates a new date object as zero time plus milliseconds:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds:</p>
<p id="demo"></p>
<script>
const d = new Date(0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds:

Wed Dec 31 1969 16:00:00 GMT-0800 (Pacific Standard Time)

01 January 1970 plus 100 000 000 000 milliseconds is approximately 03 March 1973:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>100000000000 milliseconds from Jan 1, 1970, is approximately Mar 3, 1973:</p>
<p id="demo"></p>
<script>
const d = new Date(100000000000);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

100000000000 milliseconds from Jan 1, 1970, is approximately Mar 3, 1973:

Sat Mar 03 1973 01:46:40 GMT-0800 (Pacific Standard Time)

January 01 1970 minus 100 000 000 000 milliseconds is approximately October 31 1966:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>100000000000 milliseconds from Jan 1, 1970, is approximately Oct 31, 1966:</p>
<p id="demo"></p>
<script>
const d = new Date(-100000000000);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

100000000000 milliseconds from Jan 1, 1970, is approximately Oct 31, 1966:

Mon Oct 31 1966 06:13:20 GMT-0800 (Pacific Daylight Time)

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds:</p>
<p id="demo"></p>
<script>
const d = new Date(86400000);
document.getElementById("demo").innerHTML = d;
</script>
<p>One day (24 hours) is 86,400,000 milliseconds.</p>
</body>
</html>

Result:

JavaScript new Date()

Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds:

Thu Jan 01 1970 16:00:00 GMT-0800 (Pacific Standard Time)

One day (24 hours) is 86,400,000 milliseconds.

Date Methods

When a Date object is created, a number of methods allow you to operate on it.

Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.

Date methods and time zones are covered in the next chapters.

Displaying Dates

JavaScript will (by default) output dates in full text string format:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript new Date()

Wed Aug 24 2022 11:01:24 GMT-0700 (Pacific Daylight Time)

When you display a date object in HTML, it is automatically converted to a string, with the toString() method.

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript toString()</h2>
<p>The toString() method converts a date to a string:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toString();
</script>
</body>
</html>

Result:

JavaScript toString()

The toString() method converts a date to a string:

Wed Aug 24 2022 11:06:07 GMT-0700 (Pacific Daylight Time)

The toUTCString() method converts a date to a UTC string (a date display standard).

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Date()</h2>
<p>The toUTCString() method converts a date to a UTC string (a date display standard):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toUTCString();
</script>
</body>
</html>

Result:

JavaScript Date()

The toUTCString() method converts a date to a UTC string (a date display standard):

Wed, 24 Aug 2022 18:29:25 GMT

The toDateString() method converts a date to a more readable format:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript toDateString()</h2>
<p>The toDateString() method converts a date to a date string:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
</script>
</body>
</html>

Result:

JavaScript toDateString()

The toDateString() method converts a date to a date string:

Wed Aug 24 2022

The toISOString() method converts a Date object to a string, using the ISO standard format:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript toISOString()</h2>
<p>The toISOString() method converts a date to a date string, using the ISO standard format:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toISOString();
</script>
</body>
</html>

Result:

JavaScript toISOString()

The toISOString() method converts a date to a date string, using the ISO standard format:

2022-08-24T18:30:45.916Z

Leave a Reply

Your email address will not be published. Required fields are marked *