Scrollable Nav Bar

Python Program to Convert a Given Number of Days into Years, Weeks, and Days

Problem statement: We are given a number of days, and our task is to convert this value into years, weeks, and remaining days using Python. To solve this, we first take the total number of days as input, then apply simple arithmetic calculations where 1 year = 365 days and 1 week = 7 days. After performing the conversion, the program displays the equivalent years, weeks, and days in a clear format.


Approach

This is a straightforward arithmetic problem. Use integer division and the modulo operator to extract years, then weeks from the remaining days, and finally the leftover days.

Steps:

  1. Read an integer n from the user (number of days).
  2. years = n // 365 — integer division gives full years.
  3. remaining_after_years = n % 365 — days left after removing years.
  4. weeks = remaining_after_years // 7 — full weeks from the remaining days.
  5. days = remaining_after_years % 7 — leftover days.

This method assumes fixed-length years (365 days). If you need calendar-accurate results across leap years, use datetime and relativedelta from dateutil (not shown here).


Method 1 — Using Basic Arithmetic

Using Basic Arithmetic : This method performs step-by-step calculations using integer division and modulo. It is beginner-friendly because each conversion step is clearly visible and easy to understand.

# Simple program to convert days into years, weeks and days

days_input = int(input("Enter number of days: "))

years = days_input // 365
remaining = days_input % 365
weeks = remaining // 7
days = remaining % 7

print(f"{days_input} days is equal to {years} year(s), {weeks} week(s), and {days} day(s).")
Code language: PHP (php)

Explaination : This simple script uses basic arithmetic and straightforward input/output. It’s ideal for beginners because it explicitly shows each step: calculating years, then weeks, then remaining days. Use this version to quickly test and understand the logic before refactoring into functions. For invalid (negative) inputs, consider adding a guard to prompt the user again.

Output:


Method 2 — Using a function

Using Function Approach This method wraps the conversion logic inside a reusable function. It improves code organization and makes the program easier to test and maintain.

def convert_days(total_days: int) -> tuple:
    """Return (years, weeks, days) for a given total number of days."""
    if total_days < 0:
        raise ValueError("Number of days must be non-negative")

    years = total_days // 365
    remaining = total_days % 365
    weeks = remaining // 7
    days = remaining % 7
    return years, weeks, days


if __name__ == "__main__":
    n = int(input("Enter number of days: "))
    y, w, d = convert_days(n)
    print(f"{n} days = {y} year(s), {w} week(s), and {d} day(s).")
Code language: PHP (php)

This function-based approach encapsulates the conversion logic so it’s reusable and easy to unit-test. Separating I/O from computation makes automated testing and integration simpler. It also allows handling edge cases (like negative input) centrally. Use this when you plan to call the conversion from other modules or write tests for behaviour.


Method 3 — Using divmod() for compact code

Using divmod() Technique This method uses Python’s built-in divmod() to compute quotient and remainder together. It is efficient and concise because it reduces the number of separate arithmetic operations.

n = int(input("Enter number of days: "))

years, rem = divmod(n, 365)
weeks, days = divmod(rem, 7)
print(f"{n} days = {years} year(s), {weeks} week(s), and {days} day(s)")
Code language: PHP (php)

The divmod() version is the most compact and idiomatic Python solution: it performs division and modulo in one step and returns both results as a tuple. This reduces temporary variables and keeps the logic easy to read. It’s a great choice when you want concise, maintainable code without sacrificing clarity. If you’re teaching absolute beginners, you may keep the expanded version with named steps for readability.


Explanation

All three methods use the same arithmetic idea. Divide by 365 to get complete years; use the remainder to compute weeks (divide by 7) and the final remainder gives leftover days.

Edge cases / notes:

  • Input 0 returns 0 year(s), 0 week(s), 0 day(s).
  • Negative inputs should be rejected or handled explicitly (Method 2 raises ValueError).
  • If you want calendar-accurate conversions across leap years and months, convert a start date and add the days to it using datetime.timedelta or dateutil.relativedelta.

Time and space complexity

  • Time complexity: O(1) — only a few arithmetic operations.
  • Auxiliary space: O(1) — constant extra memory.

Quick tips for learners

  • Try variations: convert days to years-months-days where you assume 1 month = 30 days.
  • Use divmod() to make the code shorter and clearer.
  • Add input validation to make the program robust.