By default, JavaScript will use the browser's time zone and display a date as a full text string.
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
new Date(year, month, ...) creates a new date object with a specified date and time.
<p id="demo1"></p>
<script>
const d1 = new Date(2018, 11, 24, 10, 33, 30, 0);
document.getElementById("demo1").innerHTML = d1;
</script>
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.
<p id="demo2"></p>
<script>
const d2 = new Date(2018, 15, 24, 10, 33, 30, 0);
document.getElementById("demo2").innerHTML = d2;
</script>
Specifying a day higher than max, will not result in an error but add the overflow to the next month.
<p id="demo3"></p>
<script>
const d3 = new Date(2018, 05, 35, 10, 33, 30, 0);
document.getElementById("demo3").innerHTML = d3;
</script>
6 numbers specify year, month, day, hour, minute, second.
<p id="demo4"></p>
<script>
const d4 = new Date(2018, 11, 24, 10, 33, 30);
document.getElementById("demo4").innerHTML = d4;
</script>
5 numbers specify year, month, day, hour, minute.
<p id="demo5"></p>
<script>
const d5 = new Date(2018, 11, 24, 10, 33);
document.getElementById("demo5").innerHTML = d5;
</script>
4 numbers specify year, month, day, hour.
<p id="demo6"></p>
<script>
const d6 = new Date(2018, 11, 24, 10);
document.getElementById("demo6").innerHTML = d6;
</script>
3 numbers specify year, month, day.
<p id="demo7"></p>
<script>
const d7 = new Date(2018, 11, 24);
document.getElementById("demo7").innerHTML = d7;
</script>
2 numbers specify year, month.
<p id="demo8"></p>
<script>
const d8 = new Date(2018, 11);
document.getElementById("demo8").innerHTML = d8;
</script>
You cannot omit month. If you supply only one parameter it will be treated as milliseconds since January 1, 1970.
<p id="demo9"></p>
<script>
const d9 = new Date(2018);
document.getElementById("demo9").innerHTML = d9;
</script>
Two digit years will be interpreted as 19xx.
<p id="demo10"></p>
<script>
const d10 = new Date(99, 11, 24);
document.getElementById("demo10").innerHTML = d10;
</script>
One digit years will be interpreted as 19xx.
<p id="demo11"></p>
<script>
const d11 = new Date(9, 11, 24);
document.getElementById("demo11").innerHTML = d11;
</script>
A Date object can be created with a specified date and time.
<p id="demo12"></p>
<script>
const d12 = new Date("October 13, 2014 11:13:00");
document.getElementById("demo12").innerHTML = d12;
</script>
Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC).
<p id="demo13"></p>
<script>
const d13 = new Date(0);
document.getElementById("demo13").innerHTML = d13;
</script>
The toUTCString() method converts a date to a UTC string (a date display standard).
<p id="demo14"></p>
<script>
const d14 = new Date(0);
document.getElementById("demo14").innerHTML = d14.toUTCString();
</script>
The toDateString() method converts a date to a more readable format.
<p id="demo15"></p>
<script>
const d15 = new Date();
document.getElementById("demo15").innerHTML = d15.toDateString();
</script>
The toISOString() method converts a Date object to a string, using the ISO standard format.
<p id="demo16"></p>
<script>
const d16 = new Date();
document.getElementById("demo16").innerHTML = d16.toISOString();
</script>
Date and time is separated with a capital T.
UTC time is defined with a capital letter Z.
<p id="demo17"></p>
<script>
const d17 = new Date("2015-03-25T12:00:00Z");
document.getElementById("demo17").innerHTML = d17;
</script>
If you want to modify the time relative to UTC, remove the Z and add +HH:MM or -HH:MM instead.
<p id="demo18"></p>
<script>
document.getElementById("demo18").innerHTML = new Date("2015-03-25T12:00:00-06:00");
</script>
Short dates are written with an "MM/DD/YYYY" syntax like this.
<p id="demo19"></p>
<script>
const d19 = new Date("03/25/2015");
document.getElementById("demo19").innerHTML = d19;
</script>
Long dates are most often written with a "MMM DD YYYY" syntax like this.
<p id="demo20"></p>
<script>
const d20 = new Date("Mar 25 2015");
document.getElementById("demo20").innerHTML = d20;
</script>
Date.parse() returns the number of milliseconds between the date and January 1, 1970.
<p id="demo21"></p>
<script>
const msec = Date.parse("March 21, 2012");
document.getElementById("demo21").innerHTML = msec;
</script>
Milliseconds can be used to convert to a date object.
<p id="demo22"></p>
<script>
let msec1 = Date.parse("March 21, 2012");
const d22 = new Date(msec1);
document.getElementById("demo22").innerHTML = d22;
</script>
The getTime() method returns the number of milliseconds since January 1, 1970.
<p id="demo23"></p>
<script>
const d23 = new Date();
document.getElementById("demo23").innerHTML = d23.getTime();
</script>
The getFullYear() method returns the full year of a date.
<p id="demo24"></p>
<script>
const d24 = new Date();
document.getElementById("demo24").innerHTML = d24.getFullYear();
</script>
The getMonth() method returns the month of a date as a number from 0 to 11.
<p id="demo25"></p>
<script>
const d25 = new Date();
document.getElementById("demo25").innerHTML = d25.getMonth() + 1;
</script>
An array can be used to display the name of the month.
<p id="demo26"></p>
<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d26 = new Date();
let month = months[d26.getMonth()];
document.getElementById("demo26").innerHTML = month;
</script>
The getDate() method returns the day of a date as a number (1-31).
<p id="demo27"></p>
<script>
const d27 = new Date();
document.getElementById("demo27").innerHTML = d27.getDate();
</script>
The getHours() method returns the hours of a date as a number (0-23).
<p id="demo28"></p>
<script>
const d28 = new Date();
document.getElementById("demo28").innerHTML = d28.getHours();
</script>
The getMinutes() method returns the minutes of a date as a number (0-59).
<p id="demo29"></p>
<script>
const d29 = new Date();
document.getElementById("demo29").innerHTML = d29.getMinutes();
</script>
The getSeconds() method returns the seconds of a date as a number (0-59).
<p id="demo30"></p>
<script>
const d30 = new Date();
document.getElementById("demo30").innerHTML = d30.getSeconds();
</script>
The getMilliseconds() method returns the milliseconds of a date as a number (0-999).
<p id="demo31"></p>
<script>
const d31 = new Date();
document.getElementById("demo31").innerHTML = d31.getMilliseconds();
</script>
The getDay() method returns the weekday as a number.
In JavaScript, the first day of the week (0) means "Sunday".
<p id="demo32"></p>
<script>
const d32 = new Date();
document.getElementById("demo32").innerHTML = d32.getDay();
</script>
The setFullYear() method sets the year of a date object.
<p id="demo33"></p>
<script>
const d33 = new Date();
d33.setFullYear(2020);
document.getElementById("demo33").innerHTML = d33;
</script>
The setFullYear() method can optionally set month and day.
<p id="demo34"></p>
<script>
const d34 = new Date();
d34.setFullYear(2020, 11, 3);
document.getElementById("demo34").innerHTML = d34;
</script>
The setMonth() method sets the month of a date object (0-11).
<p id="demo35"></p>
<script>
const d35 = new Date();
d35.setMonth(11);
document.getElementById("demo35").innerHTML = d35;
</script>
The setDate() method sets the day of a date object (1-31).
<p id="demo36"></p>
<script>
const d36 = new Date();
d36.setDate(15);
document.getElementById("demo36").innerHTML = d36;
</script>
The setDate() method can be used to add days to a date.
<p id="demo37"></p>
<script>
const d37 = new Date();
d37.setDate(d37.getDate() + 50);
document.getElementById("demo37").innerHTML = d37;
</script>
The setHours() method sets the hours of a date object (0-23).
<p id="demo38"></p>
<script>
const d38 = new Date();
d38.setHours(22);
document.getElementById("demo38").innerHTML = d38;
</script>
The setMinutes() method sets the minutes of a date object (0-59).
<p id="demo39"></p>
<script>
const d39 = new Date();
d39.setMinutes(30);
document.getElementById("demo39").innerHTML = d39;
</script>
The setSeconds() method sets the seconds of a date object (0-59).
<p id="demo40"></p>
<script>
const d40 = new Date();
d40.setSeconds(30);
document.getElementById("demo40").innerHTML = d40;
</script>
Dates can easily be compared.
The following example compares today's date with January 14, 2100.
<p id="demo41"></p>
<script>
let text;
const today = new Date();
const someday = new Date();
someday.setFullYear(2100, 0, 14);
if (someday > today) {
text = "Today is before January 14, 2100.";
} else {
text = "Today is after January 14, 2100.";
}
document.getElementById("demo41").innerHTML = text;
</script>