---
id: 6a2037a68a0bc2aef0006003
title: "Challenge 341: Birthday Countdown"
challengeType: 28
dashedName: challenge-341
---

# --description--

Given today's date and a birthday, return the number of days until the person's next birthday.

- Today's date is given as a string in `"YYYY-MM-DD"` format, with leading zeros, for example: `"2026-07-16"`.
- The birthday is given as a string in `"M/D"` format, without leading zeros, for example: `"9/7"`.
- If today is their birthday, return the number of days until their next birthday (not `0`).
- Leap years should be accounted for.

# --hints--

`daysUntilBirthday("2026-07-16", "9/7")` should return `53`.

```js
assert.equal(daysUntilBirthday("2026-07-16", "9/7"), 53);
```

`daysUntilBirthday("2026-07-16", "3/22")` should return `249`.

```js
assert.equal(daysUntilBirthday("2026-07-16", "3/22"), 249);
```

`daysUntilBirthday("2026-07-16", "7/16")` should return `365`.

```js
assert.equal(daysUntilBirthday("2026-07-16", "7/16"), 365);
```

`daysUntilBirthday("2024-02-28", "3/1")` should return `2`.

```js
assert.equal(daysUntilBirthday("2024-02-28", "3/1"), 2);
```

`daysUntilBirthday("2023-04-24", "12/30")` should return `250`.

```js
assert.equal(daysUntilBirthday("2023-04-24", "12/30"), 250);
```

`daysUntilBirthday("2024-03-01", "2/29")` should return `1460`.

```js
assert.equal(daysUntilBirthday("2024-03-01", "2/29"), 1460);
```

`daysUntilBirthday("2096-03-01", "2/29")` should return `2920`.

```js
assert.equal(daysUntilBirthday("2096-03-01", "2/29"), 2920);
```

# --seed--

## --seed-contents--

```js
function daysUntilBirthday(today, birthday) {

  return today;
}
```

# --solutions--

```js
function daysUntilBirthday(today, birthday) {
  const [year, month, day] = today.split("-").map(Number);
  const [bMonth, bDay] = birthday.split("/").map(Number);

  const todayDate = new Date(year, month - 1, day);

  function isLeapYear(y) {
    return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
  }

  function nextValidBirthday(fromYear) {
    for (let y = fromYear; y <= fromYear + 8; y++) {
      if (bMonth === 2 && bDay === 29 && !isLeapYear(y)) continue;
      const date = new Date(y, bMonth - 1, bDay);
      if (date > todayDate) return date;
    }
  }

  const nextBirthday = nextValidBirthday(year);
  const diff = nextBirthday - todayDate;
  return Math.round(diff / (1000 * 60 * 60 * 24));
}
```
