Emmanuel Akinfulubi

JavaScriptDate

Get year, month, day and time from a date in JavaScript

Beginner · 4 min read

Once you have a date, you usually need its pieces. A calendar needs the month. A receipt needs the time. A reminder app needs to know whether today is a Monday. In this tutorial you will take one date and pull out all eight of its parts, from the year down to the milliseconds, and collect them in a single object.

Along the way you will meet the two getters that look almost the same and mean different things. Mixing them up is one of the most common date bugs there is. You need to know how to make a date with new Date(year, month, day) and that the month counts from 0, so January is 0.

One getter per part

A Date has a method for each part of it. Each one starts with get, so they are called getters:

  • getFullYear() gives the year, such as 2026.
  • getMonth() gives the month, 0 to 11.
  • getDate() gives the day of the month, 1 to 31.
  • getDay() gives the day of the week, 0 (Sunday) to 6 (Saturday).
  • getHours() gives the hour, 0 to 23.
  • getMinutes() gives the minute, 0 to 59.
  • getSeconds() gives the second, 0 to 59.
  • getMilliseconds() gives the millisecond, 0 to 999.

The names are not quite consistent. It is getFullYear, not getYear, and the day of the month is getDate. You don't need to memorize them; just keep this list nearby.

A date with a time

new Date takes more than three numbers. After the year, month and day you can give the hours, minutes, seconds and milliseconds, in that order:

JavaScript

const stamp = new Date(2026, 2, 9, 14, 30, 45, 250);
console.log(stamp.toDateString()); // 'Mon Mar 09 2026'

That is 9 March 2026 (March is month 2) at 14:30, plus 45.25 seconds. This time is in local time, the time zone your computer uses.

Here is the exercise. Have a go yourself first if you like, then read on and we will build it step by step.

2 Read every field off a date

easy

Given a date, collect its year, month, day, weekday, hours, minutes, seconds and milliseconds into one object.

Stuck? Three hints, smallest first
  1. 1. There is one getter per field, and their names are not quite consistent: getFullYear, getMonth, getDate, getDay.
  2. 2. getDate is the day of the month; getDay is the day of the week.
  3. 3. getDay returns 0 for Sunday, so it is zero-based like the month.

Step 1: read the calendar parts

Start with the year, month and day of the month:

Step 1

console.log(stamp.getFullYear(), stamp.getMonth(), stamp.getDate()); // 2026 2 9

The month is 2 because March is the third month and the count starts at 0.

Step 2: the day of the week, and the easy mistake

Now say you want the day of the month and you type the shorter name:

Step 2

console.log(stamp.getDay());  // 1
console.log(stamp.getDate()); // 9

getDay() gave 1. That is not wrong, it answered a different question: 9 March 2026 is a Monday, and Monday is day 1 of the week (Sunday is 0). Code that uses getDay by mistake can look right on a few days of the month, which is why this bug can hide for weeks.

Step 3: read the time parts

The four time getters work the same way:

Step 3

console.log(stamp.getHours(), stamp.getMinutes(), stamp.getSeconds(), stamp.getMilliseconds()); // 14 30 45 250

If you leave the time out when you build a date, every one of these is 0, because the date sits at midnight.

Step 4: collect everything in one object

Now put all eight reads in a function. It takes any date and returns an object with one named property per part:

Step 4

function fields(date) {
  return {
    year: date.getFullYear(),
    month: date.getMonth(),
    day: date.getDate(),
    weekday: date.getDay(),
    hours: date.getHours(),
    minutes: date.getMinutes(),
    seconds: date.getSeconds(),
    ms: date.getMilliseconds(),
  };
}
console.log(JSON.stringify(fields(stamp)));
// {"year":2026,"month":2,"day":9,"weekday":1,"hours":14,"minutes":30,"seconds":45,"ms":250}

JSON.stringify prints the object on one line so it is easy to read here. The property names are your choice. Calling the day of the month day and the day of the week weekday means nobody reading your code has to remember which getter is which.

Taking the date as an argument matters too. You can pass fields(new Date()) to see the parts of right now, but your tests use a fixed date, so they give the same answer on every run.

The full solution

The panel builds the same stamp and runs fields on it. It also prints the current year from the live clock, which is shown but never tested. Press Run to see it.

The answer

// 9 March 2026 at 14:30:45.250, local.
const stamp = new Date(2026, 2, 9, 14, 30, 45, 250);

function fields(date) {
  return {
    year: date.getFullYear(),
    month: date.getMonth(),          // 0 to 11
    day: date.getDate(),             // 1 to 31, the day of the MONTH
    weekday: date.getDay(),          // 0 to 6, the day of the WEEK, 0 = Sunday
    hours: date.getHours(),
    minutes: date.getMinutes(),
    seconds: date.getSeconds(),
    ms: date.getMilliseconds(),
  };
}

console.log(fields(stamp));
console.log('the live clock, never asserted:', fields(new Date()).year);

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.

8 checks

test('every field, in one object', fields(stamp), { year: 2026, month: 2, day: 9, weekday: 1, hours: 14, minutes: 30, seconds: 45, ms: 250 });
test('March is month 2', fields(stamp).month, 2);
test('9 March 2026 is a Monday, so getDay is 1', fields(stamp).weekday, 1);
test('getDate is the day of the month', stamp.getDate(), 9);
test('the two are easy to confuse and rarely equal', stamp.getDate() === stamp.getDay(), false);
test('milliseconds survive', stamp.getMilliseconds(), 250);
test('an omitted time reads as zero', fields(new Date(2026, 2, 9)).hours, 0);
test('eight fields in all', Object.keys(fields(stamp)).length, 8);

getDate and getDay are one letter apart and mean different things — day of the month against day of the week. Mixing them up produces code that works for a few days a month and then silently stops, which is worse than failing outright.

There is no getWeek, no getQuarter and no getDayOfYear. Everything beyond this list you compute yourself, which is what most of this section is about.

Common mistakes

  • Using getDay() for the day of the month. It returns the weekday, 0 to 6. You want getDate().
  • Showing getMonth() to a person as it is. March comes back as 2. Add 1 before you display it.
  • Looking for getWeek() or getDayOfYear(). They don't exist. Anything beyond the eight getters you work out yourself.

Try it yourself

  1. Add a humanMonth property to fields that holds the month from 1 to 12.
  2. Build a date with no time, such as new Date(2026, 2, 9), and check that fields gives 0 for all four time parts.
  3. Write isMonday(date) that returns true only on Mondays. Which getter do you need?

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.