Emmanuel Akinfulubi

JavaScriptDate

Get the start of the week in JavaScript

Intermediate · 4 min read

Weekly reports, timesheets and calendar week views all start from one question: which day does this week begin on? The answer depends on where you are. In the United States, calendars usually start the week on Sunday. In much of Europe, and in the ISO standard for week numbers, the week starts on Monday.

In this tutorial you will write startOfWeek(date, firstDay), which works for either convention, and endOfWeek(date, firstDay). You will also meet a small math trap with negative numbers that sends the date into the wrong week. You need to know how to make a date with new Date(year, month, day), that months count from 0, and that setDate can move a date back across a month end.

getDay numbers the weekdays

getDay() returns the day of the week as a number from 0 to 6. Sunday is 0, Monday is 1, and Saturday is 6.

JavaScript

console.log(new Date(2026, 0, 15).getDay()); // 4: Thursday
console.log(new Date(2026, 0, 18).getDay()); // 0: Sunday

firstDay uses the same numbers: 0 for a Sunday-first week, 1 for a Monday-first week.

How far back to go

To find the start of the week, work out how many days you have to step back. For a Thursday (4) in a Sunday-first week (0), that is 4 minus 0, which is 4 days. In a Monday-first week (1), it is 4 minus 1, which is 3 days.

To print dates, this tutorial uses iso, which turns a date into YYYY-MM-DD text:

JavaScript

const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');

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

34 The start and end of the week, both conventions

fair

Find the first day of the week containing a date, for a week starting on Sunday and for a week starting on Monday. Then the last day.

Stuck? Three hints, smallest first
  1. 1. How far back you go is getDay() minus the first day of the week.
  2. 2. That can be negative, so add 7 and take the remainder to bring it into 0 to 6.
  3. 3. The end of the week is six days after the start, not seven.

Step 1: the simple subtraction

Copy the date, set it to midnight, and step back getDay() - firstDay days:

Step 1

function startOfWeek(date, firstDay) {
  const copy = new Date(date);
  copy.setHours(0, 0, 0, 0);
  const shift = copy.getDay() - firstDay;
  copy.setDate(copy.getDate() - shift);
  return copy;
}
const thursday = new Date(2026, 0, 15);   // 15 January 2026
console.log(iso(startOfWeek(thursday, 0))); // '2026-01-11'
console.log(iso(startOfWeek(thursday, 1))); // '2026-01-12'

11 January 2026 is a Sunday and the 12th is a Monday. Both answers look right. setHours(0, 0, 0, 0) makes the start of the week begin at midnight.

Step 2: the wrong turn, a Sunday in a Monday week

Now try Sunday 18 January in a Monday-first week:

Step 2

const sunday = new Date(2026, 0, 18);     // 18 January 2026
console.log(iso(startOfWeek(sunday, 1))); // '2026-01-19'

The 19th is the next Monday. In a Monday-first week, Sunday is the last day, so its week began on Monday the 12th. What went wrong? getDay() is 0 for Sunday, so shift is 0 minus 1, which is -1. Stepping back -1 days means stepping forward one day.

Step 3: keep the shift between 0 and 6

You might expect % to fix this, since remainders usually wrap numbers into a range. In JavaScript, though, % keeps the sign of the left side:

Step 3

console.log(-1 % 7);       // -1
console.log((-1 + 7) % 7); // 6
console.log((3 + 7) % 7);  // 3

Adding 7 first lifts any negative shift into the positive range, and % 7 brings large ones back down. The result is always 0 to 6. A Sunday in a Monday-first week becomes 6 days back, which is correct.

Here is startOfWeek with the fix:

Step 3b

function startOfWeek(date, firstDay) {
  const copy = new Date(date);
  copy.setHours(0, 0, 0, 0);
  const shift = (copy.getDay() - firstDay + 7) % 7;   // + 7 so it is never negative
  copy.setDate(copy.getDate() - shift);
  return copy;
}
console.log(iso(startOfWeek(sunday, 1))); // '2026-01-12'
console.log(iso(startOfWeek(sunday, 0))); // '2026-01-18'

In a Sunday-first week, a Sunday is its own start, so the shift is 0.

Step 4: the end of the week

The end of the week is six days after its start. Not seven: seven days later is the start of the next week.

Step 4

function endOfWeek(date, firstDay) {
  const copy = startOfWeek(date, firstDay);
  copy.setDate(copy.getDate() + 6);                   // six, not seven
  return copy;
}
console.log(iso(endOfWeek(thursday, 1))); // '2026-01-18'
console.log(iso(endOfWeek(thursday, 0))); // '2026-01-17'

A Monday-first week ends on Sunday, and a Sunday-first week ends on Saturday. startOfWeek already returns a fresh copy, so endOfWeek can change it safely.

Step 5: across a month end

setDate with a smaller day rolls back into the previous month when it needs to:

Step 5

console.log(iso(startOfWeek(new Date(2026, 1, 3), 1))); // '2026-02-02'
console.log(iso(startOfWeek(new Date(2026, 1, 1), 1))); // '2026-01-26'

Tuesday 3 February steps back to Monday 2 February. Sunday 1 February steps back six days into January.

The full solution

The panel has both helpers and prints the week of Thursday 15 January for each convention, plus the Sunday case. The checks cover both conventions, the Sunday that belongs to the previous Monday-first week, the end days, midnight and a month end. Press Run to see them.

The answer

const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');

function startOfWeek(date, firstDay) {
  const copy = new Date(date);
  copy.setHours(0, 0, 0, 0);
  const shift = (copy.getDay() - firstDay + 7) % 7;   // + 7 so it is never negative
  copy.setDate(copy.getDate() - shift);
  return copy;
}

function endOfWeek(date, firstDay) {
  const copy = startOfWeek(date, firstDay);
  copy.setDate(copy.getDate() + 6);                   // six, not seven
  return copy;
}

const thursday = new Date(2026, 0, 15);   // 15 January 2026
const sunday = new Date(2026, 0, 18);     // 18 January 2026

console.log('Thu 15/01, week from Sunday:', iso(startOfWeek(thursday, 0)), 'to', iso(endOfWeek(thursday, 0)));
console.log('Thu 15/01, week from Monday:', iso(startOfWeek(thursday, 1)), 'to', iso(endOfWeek(thursday, 1)));
console.log('Sun 18/01, week from Monday:', iso(startOfWeek(sunday, 1)), '<- last Monday, not today');

Output


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

9 checks

test('Sunday-first, the week of Thursday 15 January starts on the 11th', iso(startOfWeek(thursday, 0)), '2026-01-11');
test('Monday-first, it starts on the 12th', iso(startOfWeek(thursday, 1)), '2026-01-12');
test('Sunday-first, a Sunday is its own start', iso(startOfWeek(sunday, 0)), '2026-01-18');
test('Monday-first, that Sunday belongs to the previous week', iso(startOfWeek(sunday, 1)), '2026-01-12');
test('the end of the Monday-first week is the Sunday', iso(endOfWeek(thursday, 1)), '2026-01-18');
test('and of the Sunday-first week, the Saturday', iso(endOfWeek(thursday, 0)), '2026-01-17');
test('the start always lands on the requested weekday', startOfWeek(thursday, 1).getDay(), 1);
test('the time is set to midnight', startOfWeek(thursday, 1).getHours(), 0);
test('crossing a month end works', iso(startOfWeek(new Date(2026, 1, 3), 1)), '2026-02-02');

(getDay() - firstDay + 7) % 7 is the piece worth memorising. Without the + 7, a Sunday in a Monday-first week gives -1, and -1 % 7 in JavaScript is -1 rather than 6, so you move forward a day and land in the wrong week entirely.

The two conventions genuinely differ, and the fourth assertion is the case that catches it: a Sunday is the first day of its week in the American reading and the last day of the previous week in the ISO one. Whichever your product uses, pass it in rather than assuming — and note that the ISO week number in exercise 50 is defined only for Monday-first weeks.

Common mistakes

  • Leaving out the + 7. A Sunday in a Monday-first week moves forward into the next week.
  • Adding 7 days for the end of the week. That is the start of the next week. Add 6.
  • Hard-coding Sunday or Monday. Pass firstDay in, so the same code serves both conventions.

Try it yourself

  1. Print the Monday-first week of every day from 12 to 18 January 2026. They should all start on the 12th.
  2. Write weekDays(date, firstDay) that returns all seven dates of the week as an array.
  3. Use startOfWeek with firstDay set to 6 for a Saturday-first week. Which date does Thursday 15 January give?

Keep practising

Newsletter

New writing, when it lands.

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