18. JavaScript Dates
What you will learn
- How to create a date object
- How to read parts of a date
- How to format dates for people
- How to calculate with dates
Creating dates
The Date object stores a moment in time. Creating one with no arguments gives the current date and time from the visitor’s device.
Example
<p id="demo"></p>
<script>
const now = new Date();
document.getElementById("demo").textContent = "Right now: " + now.toString();
</script>
Output
Shows the current date and time of the person running it. Press Try it Yourself to see it.
Ways to create a specific date
| Code | Meaning |
|---|---|
new Date("2026-03-15") |
From a date string (ISO format is the safest) |
new Date(2026, 2, 15) |
Year, month (starting at 0), day |
new Date(2026, 2, 15, 14, 30) |
With hours and minutes |
new Date(milliseconds) |
Milliseconds since 1 January 1970 |
Months start at 0. January is 0 and December is 11. This is the most common date bug in JavaScript. So new Date(2026, 2, 15) is March 15, not February.
Reading parts of a date
Example
<p id="demo"></p>
<script>
const d = new Date(2026, 2, 15, 14, 30);
document.getElementById("demo").innerHTML =
"Year: " + d.getFullYear() + "<br>" +
"Month (0-11): " + d.getMonth() + "<br>" +
"Day of month: " + d.getDate() + "<br>" +
"Day of week (0 = Sunday): " + d.getDay() + "<br>" +
"Hours: " + d.getHours() + "<br>" +
"Minutes: " + d.getMinutes();
</script>
Output (after the script runs)
Year: 2026
Month (0-11): 2
Day of month: 15
Day of week (0 = Sunday): 0
Hours: 14
Minutes: 30
Note the odd names: getDate() is the day of the month, and getDay() is the day of the week.
Formatting for people
toLocaleDateString() formats a date in the style of a language and region, which is far better than building text yourself.
Example
<p id="demo"></p>
<script>
const d = new Date(2026, 2, 15);
document.getElementById("demo").innerHTML =
"US: " + d.toLocaleDateString("en-US") + "<br>" +
"UK: " + d.toLocaleDateString("en-GB") + "<br>" +
"Long: " + d.toLocaleDateString("en-US", {
weekday: "long", year: "numeric", month: "long", day: "numeric"
});
</script>
Output (after the script runs)
US: 3/15/2026
UK: 15/03/2026
Long: Sunday, March 15, 2026
Use toLocaleTimeString() for times, and toISOString() to get a standard text form such as 2026-03-15T09:30:00.000Z that is good for storing or sending.
Changing dates
The set methods change a date. JavaScript fixes overflow for you: adding 30 days to the 15th moves into the next month correctly.
Example
<p id="demo"></p>
<script>
const start = new Date(2026, 0, 20); // 20 January 2026
const later = new Date(start); // a copy
later.setDate(later.getDate() + 30); // add 30 days
const fmt = { year: "numeric", month: "long", day: "numeric" };
document.getElementById("demo").textContent =
start.toLocaleDateString("en-US", fmt) + " + 30 days = " +
later.toLocaleDateString("en-US", fmt);
</script>
Output (after the script runs)
January 20, 2026 + 30 days = February 19, 2026
Notice the copy: new Date(start). The set methods change the original, so copy first if you still need it.
Comparing and subtracting dates
Dates can be compared with < and >. Subtracting two dates gives milliseconds, which you can convert.
Example
<p id="demo"></p>
<script>
const a = new Date(2026, 0, 1);
const b = new Date(2026, 11, 25);
const msPerDay = 1000 * 60 * 60 * 24;
const days = Math.round((b - a) / msPerDay);
document.getElementById("demo").textContent =
"Days from 1 Jan to 25 Dec: " + days + " | b is later: " + (b > a);
</script>
Output (after the script runs)
Days from 1 Jan to 25 Dec: 358 | b is later: true
Time zones
A date shows in the time zone of the visitor’s device, so two people may see different clock times for the same moment. For stored or shared values, use toISOString() (which uses UTC), and format for display only when you show it. Dates are tricky in general. For heavy date work, many developers use a tested library.
Common mistakes
- Forgetting that months start at 0.
- Confusing
getDate()(day of month) withgetDay()(day of week). - Changing a date with
setmethods when you needed the original. - Parsing unusual date strings such as
"15/03/2026", which browsers read differently. UseYYYY-MM-DD. - Assuming every visitor has the same time zone.
Exercise
Show the name of the weekday on which 20 May 2000 fell, for example Saturday. Use toLocaleDateString with the option { weekday: "long" }.
Show solution
<p id="demo"></p>
<script>
const birthday = new Date(2000, 4, 20);
document.getElementById("demo").textContent =
birthday.toLocaleDateString("en-US", { weekday: "long" });
</script>
The result is Saturday.
Knowledge check
1. What number is March in JavaScript’s month numbering?
2, because months start at 0.
2. What do you get when you subtract one date from another?
The difference in milliseconds.
3. Which method formats a date for a specific region?
toLocaleDateString()
Summary
new Date()gives the current moment, and you can also build specific dates.- Months start at 0, and the
getandsetmethods read and change parts. - Format with
toLocaleDateString(), and store withtoISOString().