Back to Freecodecamp

Challenge 232: Due Date

curriculum/challenges/english/blocks/daily-coding-challenges-python/69b1028d6e265413d0198a2b.md

latest1.7 KB
Original Source

--description--

Given a date string, return the date 9 months in the future.

  • The given and return strings have the format "YYYY-MM-DD".
  • If the month nine months into the future doesn't contain the original day number, return the last day of that month.

--hints--

get_due_date("2025-03-30") should return "2025-12-30".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2025-03-30"), "2025-12-30")`)
}})

get_due_date("2025-04-27") should return "2026-01-27".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2025-04-27"), "2026-01-27")`)
}})

get_due_date("2025-05-29") should return "2026-02-28".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2025-05-29"), "2026-02-28")`)
}})

get_due_date("2026-06-30") should return "2027-03-30".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2026-06-30"), "2027-03-30")`)
}})

get_due_date("2026-10-11") should return "2027-07-11".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2026-10-11"), "2027-07-11")`)
}})

--seed--

--seed-contents--

py
def get_due_date(date_str):

    return date_str

--solutions--

py
import calendar

def get_due_date(date_str):
    year, month, day = map(int, date_str.split("-"))

    month += 9
    year += (month - 1) // 12
    month = (month - 1) % 12 + 1

    days_in_month = calendar.monthrange(year, month)[1]

    if day > days_in_month:
        day = days_in_month

    return f"{year}-{month:02d}-{day:02d}"