Emmanuel Akinfulubi

JavaScriptBasic

Get the day of the week from a date in JavaScript

Beginner · 3 min read

Calendars, booking forms and delivery apps all need to know what day of the week a date falls on. Is the shop open on that day? Is it a weekend? Should the alarm ring? In this tutorial you will turn any date into its day name, like "Wednesday", and then use the same idea to spot weekends.

You will write dayName(date), which returns the name of the day, and isWeekend(date), which returns true or false. You need to know what a function and an array are, and that new Date(2026, 8, 2) means 2 September 2026 (months count from 0).

Two methods that look alike

A Date has two methods whose names differ by a single letter:

  • getDay() gives the day of the week, as a number from 0 to 6.
  • getDate() gives the day of the month, from 1 to 31.

JavaScript

const d = new Date(2026, 8, 2); // Wednesday 2 September 2026
console.log(d.getDay());  // 3
console.log(d.getDate()); // 2

Mixing them up is a classic bug, and because both return small numbers, the wrong one often looks believable.

Sunday comes first

getDay() numbers the week starting from Sunday:

Output

0 Sunday   1 Monday   2 Tuesday   3 Wednesday
4 Thursday 5 Friday   6 Saturday

Many people expect Monday to be first, or the numbers to start at 1. In JavaScript neither is true. The international ISO standard numbers Monday as 1 and Sunday as 7, so if your code shares weekday numbers with a server, check which way it counts.

Here is the exercise. Try it yourself first if you like, then follow along.

10 Find a date’s day of the week

easy

Given a date, produce the name of its day. Handle the numbering correctly.

Stuck? Three hints, smallest first
  1. 1. getDay() returns 0 to 6 with Sunday as 0, which is not what most people expect.
  2. 2. getDate() is a completely different thing: the day of the month.
  3. 3. Mixing up the two is a classic bug. Name your variables so it cannot happen.

Step 1: a list of names in the right order

Since getDay() gives a number from 0 to 6, put the names in an array in that exact order. Then the number is the position in the array.

Step 1

const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
console.log(DAY_NAMES[0]); // Sunday
console.log(DAY_NAMES[3]); // Wednesday

The name DAY_NAMES is in capitals because it is a fixed list that never changes. That is a common way to mark a constant.

Step 2: look the date up

Step 2

function dayName(date) {
  return DAY_NAMES[date.getDay()];
}
console.log(dayName(new Date(2026, 8, 2))); // Wednesday
console.log(dayName(new Date(2026, 0, 1))); // Thursday
console.log(dayName(new Date(2000, 0, 1))); // Saturday

1 January 2000 really was a Saturday, so this is an easy date to check against.

Step 3: the wrong method

Now see what happens if you use getDate() by mistake:

JavaScript

console.log(DAY_NAMES[new Date(2026, 8, 2).getDate()]);  // Tuesday, but it was a Wednesday
console.log(DAY_NAMES[new Date(2026, 8, 25).getDate()]); // undefined

On the 2nd you get an answer that looks fine but is wrong. From the 7th onward there is no such position in the array, and you get undefined. If you test only on early-month dates, you may not notice.

Step 4: use both, on purpose

Sometimes you want both numbers. Clear names keep them apart:

Step 4

function describe(date) {
  return `${dayName(date)} the ${date.getDate()}`;
}
console.log(describe(new Date(2026, 0, 1))); // Thursday the 1

Step 5: is it a weekend?

Saturday is 6 and Sunday is 0. So a date is on a weekend when its getDay() is either of those. || means "or".

Step 5

function isWeekend(date) {
  const n = date.getDay();
  return n === 0 || n === 6;
}
console.log(isWeekend(new Date(2026, 8, 2))); // false (Wednesday)
console.log(isWeekend(new Date(2026, 8, 5))); // true (Saturday)

n === 0 || n === 6 is already true or false, so the function returns it directly.

The full solution

The complete answer prints three dates with their day names and day of the month, then checks two dates for the weekend. Press Run to see the output.

The answer

const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

function dayName(date) {
  return DAY_NAMES[date.getDay()];      // getDay: 0..6, Sunday first
}

// Whereas getDate is the day of the MONTH. Different question entirely.
function describe(date) {
  return `${dayName(date)} the ${date.getDate()}`;
}

const dates = [
  new Date(2026, 8, 2),     // Wednesday 2 September 2026
  new Date(2026, 0, 1),     // Thursday 1 January 2026
  new Date(2000, 0, 1),     // Saturday 1 January 2000
];

for (const d of dates) console.log(describe(d));

// Weekday or weekend, using the numbering directly.
function isWeekend(date) {
  const n = date.getDay();
  return n === 0 || n === 6;
}

console.log('2 Sep 2026 a weekend?', isWeekend(dates[0]));
console.log('5 Sep 2026 a weekend?', isWeekend(new Date(2026, 8, 5)));

Output


      
7 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 7 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

7 checks

test('a known Wednesday', dayName(dates[0]), 'Wednesday');
test('a known Thursday', dayName(dates[1]), 'Thursday');
test('the millennium was a Saturday', dayName(dates[2]), 'Saturday');
test('getDay and getDate are different', dates[0].getDay() === dates[0].getDate(), false);
test('describe uses both correctly', describe(dates[1]), 'Thursday the 1');
test('Saturday counts as the weekend', isWeekend(new Date(2026, 8, 5)), true);
test('Wednesday does not', isWeekend(dates[0]), false);

getDay() and getDate() are one letter apart and answer entirely different questions — day of the week versus day of the month. The fourth assertion exists purely to make that concrete. Note also that getDay() puts Sunday at 0, whereas the ISO standard puts Monday at 1, so any code sharing weekday numbers with a backend needs to agree which convention it is using.

Common mistakes

  • Using getDate() instead of getDay(). You get the day of the month, which gives wrong names and, after the 6th, undefined.
  • Starting the name list with Monday. Every name comes out one day off, because JavaScript's 0 is Sunday.
  • Testing weekends with n === 5 || n === 6. That is Friday and Saturday. Saturday is 6 and Sunday is 0.

Try it yourself

  1. Write shortDayName(date) that returns "Wed", "Thu" and so on. Can you reuse DAY_NAMES with slice(0, 3)?
  2. Find out which day of the week you were born on.
  3. Write isoWeekday(date) that returns 1 for Monday through 7 for Sunday. (Hint: getDay() is 0 only on Sunday.)

Keep practising

Newsletter

New writing, when it lands.

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