Python Forum
How To Create A "Birthday Calculator" in Python? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How To Create A "Birthday Calculator" in Python? (/thread-35199.html)



How To Create A "Birthday Calculator" in Python? - unigueco9 - Oct-08-2021

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.


RE: How To Create A "Birthday Calculator" in Python? - Larz60+ - Oct-08-2021

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



RE: How To Create A "Birthday Calculator" in Python? - Underscore - Oct-11-2021

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)")



RE: How To Create A "Birthday Calculator" in Python? - SamHobbs - Oct-11-2021

If the birth day and month have already passed for the current year then use the following year.