đŸ› ïžToolsShed

Next Birthday Calculator

Calculate days until your next birthday, current age, and day of the week.

HĂ€ufig gestellte Fragen

Code-Implementierung

from datetime import date

def next_birthday_info(birth_date: date, today: date = None) -> dict:
    """Calculate days until next birthday, current age, and day of week."""
    if today is None:
        today = date.today()

    age = today.year - birth_date.year
    # Check if birthday already happened this year
    had_birthday = (today.month, today.day) >= (birth_date.month, birth_date.day)
    if not had_birthday:
        age -= 1

    # Next birthday
    next_year = today.year if not had_birthday else today.year + 1
    try:
        next_bday = date(next_year, birth_date.month, birth_date.day)
    except ValueError:  # Feb 29 on non-leap year
        next_bday = date(next_year, 3, 1)

    days_until = (next_bday - today).days
    day_of_week = next_bday.strftime("%A")

    return {
        "current_age": age,
        "next_birthday": next_bday.isoformat(),
        "days_until": days_until,
        "day_of_week": day_of_week,
        "is_today": days_until == 0,
    }

info = next_birthday_info(date(1990, 7, 15))
print(f"Age: {info['current_age']}")
print(f"Next birthday: {info['next_birthday']} ({info['day_of_week']})")
print(f"Days until: {info['days_until']}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.