Python Forum

Full Version: How To Create A "Birthday Calculator" in Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm relatively new to Python and I want to create a program with Python that can calculate how many days are left until your birthday. However I'm stuck because someone's birthday may have already happened this year and then the program doesn't work for them and I can't find a way to make it dynamic without having to edit the date everyday. Can someone offer a fix? Highly appreciated, thanks.
The following page explains, using examples, most of the datetime methods: https://pymotw.com/3/datetime/

here are some helpers:

import datetime

# to get today's date
today = datetime.date.today()

# to calculate date for Feb 29, 2012 (a leap year)
thedate = datetime.date(2012, 2, 29)

# to get delta in days form today (this is an absolute value)
delta = today - thedate

print(f"The delta is {delta.days} days")
Output:
The delta is 3509 days
its just as simple as asking the user if they're birthday has passed this year
birthday_has_pass_this_year = input("has your birthday passed this year? (y/n)")
If the birth day and month have already passed for the current year then use the following year.