Emmanuel Akinfulubi

JavaScriptDate

Add weeks to a date in JavaScript

Beginner · 4 min read

Weekly meetings, a bin collection every Tuesday, "your parcel arrives in two weeks": many real schedules move in whole weeks. In this tutorial you will write addWeeks(date, weeks), which returns a new date that many weeks later, or earlier when the count is negative.

Weeks are the friendliest unit in date math. A week is always seven days, so the code is tiny. You will also check two facts that matter for planning: adding weeks never changes the day of the week, and 52 weeks is not quite a year. You need to know how to make a date with new Date(year, month, day) and that months count from 0.

Build on addDays

If you can add days, you can add weeks: one week is seven days. Here is an addDays helper. It copies the date, moves the copy's day of the month on, and lets JavaScript roll any extra days into the next month:

JavaScript

function addDays(date, days) {
  const copy = new Date(date);
  copy.setDate(copy.getDate() + days);
  return copy;
}

The copy matters: setDate changes the date it is called on, and you don't want to move the caller's date by accident.

getDay and the day of the week

getDay() tells you the day of the week as a number. Sunday is 0, Monday is 1, and so on up to Saturday, which is 6.

JavaScript

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

To print dates in an easy-to-compare form, this tutorial uses a small iso helper that turns a date into YYYY-MM-DD text using the local getters:

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 together.

23 Add weeks

easy

Add and subtract whole weeks. Show that the weekday never changes, and that 52 weeks is not a year.

Stuck? Three hints, smallest first
  1. 1. A week is seven days, so this is addDays with the count multiplied.
  2. 2. Adding a multiple of seven days cannot change getDay().
  3. 3. 52 weeks is 364 days, one or two short of a year.

Step 1: one line on top of addDays

Multiply the number of weeks by seven and pass it on:

Step 1

const addWeeks = (date, weeks) => addDays(date, weeks * 7);

const thursday = new Date(2026, 0, 15);   // 15 January 2026
console.log(iso(addWeeks(thursday, 1)));  // '2026-01-22'
console.log(iso(addWeeks(thursday, -2))); // '2026-01-01'

One week on is the 22nd. Two weeks back is minus fourteen days, which lands on 1 January. Because addWeeks calls addDays, it also copies the date first, so thursday stays where it was.

Step 2: the weekday never changes

Seven days on from a Thursday is a Thursday. So is 14 days, or 259. Any whole number of weeks keeps the same weekday:

Step 2

console.log(addWeeks(thursday, 37).getDay()); // 4
console.log(thursday.getDay());               // 4

That is a handy check when you test a weekly schedule. If the weekday ever changes, something is adding days that aren't a multiple of seven.

Step 3: month and year ends

addDays already handles rollover, so weeks cross month and year ends without any extra code:

Step 3

console.log(iso(addWeeks(new Date(2026, 11, 31), 1))); // '2027-01-07'

31 December plus seven days asks for day 38 of December. JavaScript carries the extra into January 2027 and lands on the 7th.

Step 4: 52 weeks is not a year

Here is the trap. A year has "about 52 weeks", so it is tempting to plan a year ahead with addWeeks(date, 52):

Step 4

const newYear = new Date(2026, 0, 1);
console.log(iso(addWeeks(newYear, 52))); // '2026-12-31'
console.log(iso(addWeeks(newYear, 53))); // '2027-01-07'

52 weeks is 364 days. A normal year has 365 days and a leap year has 366, so 52 weeks falls one or two days short. 53 weeks overshoots by about a week.

This matters for a yearly schedule. If you add 52 weeks each year, the date slips back a day or two every year. After a few years, the event has drifted to a different week. For something that happens once a year, count in years. For something weekly, accept that some years contain 53 of that weekday.

The full solution

The panel builds addWeeks on top of addDays, then checks forward and backward moves, the unchanged weekday, the year end and the 52-week case. Press Run to see every result.

The answer

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

function addDays(date, days) {
  const copy = new Date(date);
  copy.setDate(copy.getDate() + days);
  return copy;
}

const addWeeks = (date, weeks) => addDays(date, weeks * 7);

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

console.log('+1 week  ->', iso(addWeeks(thursday, 1)));
console.log('-2 weeks ->', iso(addWeeks(thursday, -2)));
console.log('+52 weeks->', iso(addWeeks(new Date(2026, 0, 1), 52)), '<- still 2026');

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 week on', iso(addWeeks(thursday, 1)), '2026-01-22');
test('two weeks back', iso(addWeeks(thursday, -2)), '2026-01-01');
test('the weekday is unchanged, whatever the count', addWeeks(thursday, 37).getDay(), thursday.getDay());
test('a week on from the end of the year', iso(addWeeks(new Date(2026, 11, 31), 1)), '2027-01-07');
test('52 weeks from 1 January 2026 is 31 December 2026', iso(addWeeks(new Date(2026, 0, 1), 52)), '2026-12-31');
test('so 52 weeks is 364 days, not a year', iso(addWeeks(new Date(2026, 0, 1), 52)) === '2027-01-01', false);
test('and 53 weeks overshoots into the next year', iso(addWeeks(new Date(2026, 0, 1), 53)), '2027-01-07');

Weeks are the one unit of calendar arithmetic with no awkward cases: seven days is seven days, so the weekday is preserved and no month-end clamping is needed.

The 52-week assertion matters when someone asks for "a year of weekly reports". 52 weeks is 364 days, so an annual schedule built by adding weeks drifts back by a day or two each year and eventually loses a whole week. If the schedule is annual, count in years; if it is weekly, accept that some years contain 53 of them.

Common mistakes

  • Forgetting to multiply by seven. addDays(date, weeks) adds days, not weeks. Check that the weekday stays the same.
  • Using 52 weeks for "a year from now". It lands one or two days early. Add a year instead.
  • Changing the input date. If your version calls setDate on the date it was given, the caller's date moves too. Copy first.

Try it yourself

  1. List the next six Thursdays after 15 January 2026 with a loop and addWeeks.
  2. Write weeksBefore(date, n) that uses addWeeks with a negative count.
  3. Starting on 1 January 2026, how many times can you add a week before the year changes? Write a loop that counts.

Keep practising

Newsletter

New writing, when it lands.

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