Python datetime – Birthday countdown

In todays post we will be creating a script that will countdown the number of days till the users birthday. To do this we will be using the ‘datetime’ module which supplies classes for manipulating dates and times.

We start by taking the users birthday as input and then pass this to our ‘get_days’ function. Here we will parse the birthday and use the minus operator to get the difference between the users birthday and todays date.

Full source code can be found below.

from datetime import datetime

def get_days(Birthday):
    Today = datetime.now()
    Birthday = datetime.strptime(Birthday, "%Y-%m-%d")
    return abs((Birthday - Today).days)

Birthday = input("When is you next birthday? (YYYY-MM-DD) ")
print("Your Birthday is "+ str(get_days(Birthday)) +" days away!!")

Leave a Reply