- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Dates
JavaScript Dates
JavaScript Dates
The Date object represents a single moment in time.
A date is stored as the number of milliseconds since the start of 1970.
Everything else is a way of reading or displaying that number.
Example
Example
javascript
const date = new Date(2026, 0, 15);
console.log(date.getFullYear());
console.log(date.getDate());The output is 2026 then 15.
Creating a Date
new Date() with no arguments gives the current moment.
new Date(year, month, day) builds a specific date.
new Date("2026-01-15") parses an ISO string.
Syntax
Syntax
javascript
new Date(year, month, day);Months start at 0, which is the classic trap.
Months Start at Zero
January is 0 and December is 11.
Days of the month, confusingly, start at 1.
Example
Example
javascript
const january = new Date(2026, 0, 15);
const december = new Date(2026, 11, 15);
console.log(january.getMonth());
console.log(december.getMonth());
console.log(january.getDate());The output is 0, 11, 15.
Parsing a String
The ISO format is the only one that behaves consistently everywhere.
Note that an ISO date string is treated as UTC.
Example
Example
javascript
const date = new Date("2026-01-15T00:00:00Z");
console.log(date.getUTCFullYear());
console.log(date.getUTCMonth());
console.log(date.getUTCDate());Using the UTC readers avoids any timezone surprise.
Timestamps
getTime gives the underlying number of milliseconds.
Example
Example
javascript
const date = new Date(Date.UTC(2026, 0, 15));
console.log(typeof date.getTime());
console.log(date.getTime() > 0);Date.now() gives the current timestamp directly.
Reading the Parts
Each part has its own getter.
Example
Example
javascript
const date = new Date(Date.UTC(2026, 0, 15, 14, 30));
console.log(date.getUTCHours());
console.log(date.getUTCMinutes());
console.log(date.getUTCDay());getUTCDay gives the weekday, where 0 is Sunday.
Dates Are Mutable
The setters change the date in place, unlike most modern JavaScript.
Copy it first if the original matters.
Example
Example
javascript
const original = new Date(Date.UTC(2026, 0, 15));
const copy = new Date(original.getTime());
copy.setUTCDate(20);
console.log(original.getUTCDate());
console.log(copy.getUTCDate());The output is 15 then 20.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Dates</title>
</head>
<body>
<h1>JavaScript Dates</h1>
<p id="out"></p>
<script>
const date = new Date(2026, 0, 15);
document.getElementById("out").innerHTML =
"Year: " + date.getFullYear() +
"<br>Month number: " + date.getMonth() + " (January is 0)" +
"<br>Day of month: " + date.getDate() +
"<br>Today is: " + new Date().getFullYear();
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try December:
Change the month to 11 and check the number that appears.
Important Points
- A Date is a moment stored as milliseconds since 1970.
- Months start at 0; days of the month start at 1.
- The ISO string format parses consistently.
getTimegives the raw timestamp.- Dates are mutable, so copy before changing.
Conclusion
The zero-based month is the single biggest source of date bugs.
Working in UTC removes a whole class of timezone confusion.
For anything complex, a dedicated date library is worth the weight.
