Create a date from year, month and day in JavaScript
Beginner · 4 min read
Almost every app deals with dates: a booking form, the due date on a task, a birthday on a profile. In JavaScript you hold a date in a Date object. In this tutorial you will make one for a fixed day, 15 January 2026, from its three parts: the year, the month and the day. Then you will read those parts back out.
One surprise is waiting, and it catches nearly everyone the first time. It is the month. By the end you will know what the surprise is, and you will have a small helper function that hides it for good. You only need to know how to call a function and use console.log.
What a Date object is
A Date is a JavaScript object that holds one moment in time. You make one with the word new:
const now = new Date();
console.log(typeof now); // 'object'
With nothing in the brackets, you get the current moment, read from your computer's clock. That value changes every time you run the code. It is fine for showing the time, but you can't write a test against it, because the right answer keeps moving. In this exercise you build a date from numbers you choose, so the answer is the same every time.
Getters: reading the parts back
A date has methods that each read one part of it. They are called getters, because their names start with get:
getFullYear()gives the year, such as2026.getMonth()gives the month.getDate()gives the day of the month, such as15.
Look at that last name carefully. getDate means "the day of the month", not the whole date.
Here is the exercise. Try it on your own first if you like, or read on and we will build it together.
1 Build a date from year, month and day
easyMake a Date for 15 January 2026 from its parts, then read the parts back out. Watch the month.
Stuck? Three hints, smallest first
- 1.
new Date(year, month, day)takes numbers, and the month is not the one you would write down. - 2. January is
0and December is11, while the day of the month still starts at1. - 3. If a 1-to-12 month is what your callers have, wrap the constructor rather than making them subtract.
Step 1: build the date from its parts
new Date(year, month, day) takes three numbers. Try the obvious thing for 15 January, with January as month 1:
const guess = new Date(2026, 1, 15);
console.log(guess.toDateString()); // 'Sun Feb 15 2026'
toDateString() turns a date into short, readable text. And it says February. There is no error and no warning. The code ran fine; it just made a different day from the one you meant.
Step 2: count the month from zero
In a Date, months are counted from zero. January is month 0, February is month 1, and December is month 11. The other two parts are normal: the day of the month starts at 1, and the year is the real year.
const jan15 = new Date(2026, 0, 15);
const dec25 = new Date(2026, 11, 25);
console.log(jan15.toDateString()); // 'Thu Jan 15 2026'
console.log(dec25.toDateString()); // 'Fri Dec 25 2026'
Now both dates are the ones you asked for.
Step 3: read the parts back out
Use the three getters on jan15:
console.log(jan15.getFullYear(), jan15.getMonth(), jan15.getDate()); // 2026 0 15
The month comes back as 0, the same way it went in. So whenever you show a month number to a person, add 1 first.
You didn't give a time, so the date sits at midnight at the very start of that day. That midnight is in local time, which means the time zone your computer is set to.
console.log(jan15.getHours(), jan15.getMinutes(), jan15.getSeconds()); // 0 0 0
Step 4: a helper that takes a normal month
People, forms and files almost always give you a month from 1 to 12. Instead of remembering to subtract 1 every time, do it once, inside a function:
function makeDate(year, humanMonth, day) {
return new Date(year, humanMonth - 1, day);
}
console.log(makeDate(2026, 1, 15).getTime() === jan15.getTime()); // true
console.log(makeDate(2026, 12, 25).getMonth()); // 11
getTime() gives a date as one number: the milliseconds since the start of 1970. Two dates with the same getTime() are the same moment, so this is how you check that makeDate built the right day. Now the off-by-one lives in a single line, and the rest of your code can use the months people actually write.
The full solution
The panel has both dates, the makeDate helper and a line that prints today's date from the live clock (shown, but never tested). Press Run to see the output and the checks.
// The month is ZERO-BASED. January is 0, December is 11.
const jan15 = new Date(2026, 0, 15); // 15 January 2026, local midnight
const dec25 = new Date(2026, 11, 25); // 25 December 2026
// A wrapper that takes the month the way a human writes it, 1 to 12.
function makeDate(year, humanMonth, day) {
return new Date(year, humanMonth - 1, day);
}
console.log('parts of 15/01/2026 ->', jan15.getFullYear(), jan15.getMonth(), jan15.getDate());
console.log('as a string ->', jan15.toDateString());
// The live clock, shown and never asserted: it is different every time you run.
console.log('right now ->', new Date().toDateString());
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('the year reads back as written', jan15.getFullYear(), 2026);
test('January is month 0, not 1', jan15.getMonth(), 0);
test('but the day of the month starts at 1', jan15.getDate(), 15);
test('December is month 11', dec25.getMonth(), 11);
test('an omitted time means local midnight', [jan15.getHours(), jan15.getMinutes(), jan15.getSeconds()], [0, 0, 0]);
test('the 1-to-12 wrapper agrees', makeDate(2026, 1, 15).getTime(), jan15.getTime());
test('and reads 12 as December', makeDate(2026, 12, 25).getTime(), dec25.getTime());
test('so passing 1 straight in really means February', new Date(2026, 1, 15).getMonth(), 1);
The zero-based month is the single most common date bug in JavaScript, and it is asymmetric: the month counts from 0 while the day counts from 1 and the year is the real year. Nothing warns you, because new Date(2026, 1, 15) is a perfectly valid 15 February.
Two habits remove the problem. Convert at the boundary, as makeDate does, so the off-by-one lives in one line instead of scattered through the code. And build from parts rather than from a string when you mean a local date — new Date(2026, 0, 15) is local midnight in whatever zone the machine is in, which is usually what a calendar date means.
Common mistakes
- Passing the month you would write down.
new Date(2026, 1, 15)is 15 February, and nothing tells you so. Subtract 1, or use a helper likemakeDate. - Mixing up
getDateandgetDay.getDate()is the day of the month.getDay()is the day of the week, from 0 for Sunday to 6 for Saturday. - Testing against
new Date(). The current time is different on every run, so a check against it can't be trusted. Build fixed dates from parts.
Try it yourself
- Use
makeDateto build your next birthday and print it withtoDateString(). - Write
humanMonth(date)that returns the month from 1 to 12 instead of 0 to 11. - Print
new Date(2026, 0, 15).getDay(). Which day of the week does the number stand for?