跳到内容
🛠️ToolsShed

Next Birthday Calculator

计算到下一个生日的天数、当前年龄和星期几。

关于此工具

生日是我们每年都要庆祝的重要日子,但精确计算距离下次生日还有多少天对于策划特殊活动、通知亲朋好友或仅仅满足对距离下次庆祝还有多久的好奇心都出人意料地有用。这个Next Birthday Calculator基于你的出生日期和今天的日期,能立即计算距离下次生日还有多少天、显示你当前的年龄,甚至还能告诉你下次生日是星期几。

使用这个工具非常简单:输入你的出生日期,它会立即显示距离下次生日还有多少天、你的确切年龄(以年为单位)以及即将到来的生日的星期几。无论你的生日在什么时候,无论是几个月之后还是几周之后,它都能正常工作,非常适合策划惊喜派对、安排与生日相关的活动或提前设置提醒通知。

常见问题

代码实现

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.