Date.prototype.getDay()

Return the day of the week for a date, where 0 is Sunday, according to local time.

Signature
getDay()
  • Added in JavaScript ES1

Return value

numberAn integer between 0 and 6 representing the day of the week for the given date according to local time: 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, 4 for Thursday, 5 for Friday, 6 for Saturday. Returns NaN if the date is invalid.

Errors

Throws a TypeError if called on a value that is not a Date object.

Returns which weekday a date falls on, in local time, as a zero-based index with Sunday at 0. Like getMonth(), the value is designed to index an array of names directly.

When to reach for it

Use it to decide whether a date is a weekend, to find the next occurrence of a weekday, to align a calendar grid's first cell, or to skip non-business days when counting delivery times.

What trips people up

Sunday is 0, not Monday. Most of the world — and the ISO 8601 standard — treats Monday as day 1 and Sunday as day 7. Any week that must start on Monday needs a conversion such as (getDay() + 6) % 7, which maps Monday to 0 through Sunday to 6. Getting this wrong shifts an entire calendar grid by one column, or puts Sunday in the wrong week.

getDay() is read-only. There is no setDay(); to move to a particular weekday you compute the offset and apply it with setDate().

It is also easily confused with getDate(), which returns the day of the month. A value that never exceeds 6 is a strong sign you have the wrong method.

For displaying a weekday to a user, Intl.DateTimeFormat with { weekday: "long" } gives a localized name instead of a hand-maintained array.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Decide whether a date falls on a weekend before scheduling work
  • Find the next Monday from any starting date
  • Offset the first cell of a month-view calendar grid
  • Count business days between two dates

Frequently asked questions

Why does getDay() return 0 for Sunday instead of Monday?

The numbering is zero-based with Sunday first, following the US convention baked into the language since ES1. Convert to ISO numbering, where Monday is 1, with ((date.getDay() + 6) % 7) + 1.

How do I check if a date is a weekend in JavaScript?

Test whether getDay() is 0 or 6, which covers Sunday and Saturday. Be aware that the weekend falls on different days in some regions, so hardcoding 0 and 6 is a locale assumption.

Is there a setDay() method?

No — getDay() is read-only. To move a date to a particular weekday, compute the day offset and apply it with setDate(date.getDate() + offset).

More examples

Weekends and Monday-first weeks

1 January 2024 was a Monday, so getDay() runs 1..6 then wraps to 0. The ISO conversion produces a clean 1..7, and the weekend filter picks out the 6th and 7th.

Example · javascript
const isWeekend = (d) => d.getDay() === 0 || d.getDay() === 6;
const isoDay = (d) => ((d.getDay() + 6) % 7) + 1; // Monday = 1

const week = [1, 2, 3, 4, 5, 6, 7].map((n) => new Date(2024, 0, n));

console.log(week.map((d) => d.getDay()));
console.log(week.map(isoDay));
console.log(week.filter(isWeekend).map((d) => d.getDate()));

Next occurrence of a weekday

The modulo keeps the offset in 0-6 and the || 7 forces a full week forward when the target is today, so "next Wednesday" skips ahead instead of returning the same date.

Example · javascript
function nextWeekday(from, target) {
  const d = new Date(from);
  const delta = ((target - d.getDay() + 7) % 7) || 7;
  d.setDate(d.getDate() + delta);
  return d;
}

const wed = new Date(2024, 0, 3); // Wednesday
console.log(nextWeekday(wed, 1).toDateString()); // next Monday
console.log(nextWeekday(wed, 5).toDateString()); // next Friday
console.log(nextWeekday(wed, 3).toDateString()); // next Wednesday, not today

Aligning a calendar grid

1 September 2024 is a Sunday, so a Sunday-first grid needs 0 leading blanks while a Monday-first grid needs 6. The last line gives the 30 cells to fill.

Example · javascript
const year = 2024, month = 8; // September 2024
const first = new Date(year, month, 1);
const blanks = first.getDay();              // Sunday-first grid
const blanksMonFirst = (first.getDay() + 6) % 7;

console.log(first.toDateString());
console.log(blanks);
console.log(blanksMonFirst);
console.log(new Date(year, month + 1, 0).getDate());

Discussion

  • Be the first to comment on this lesson.