-1

The task I was given is to create a program that will check if the current date is your birthday. If it is, print "Happy Birthday!". If not then output how many days left till your birthday.

I have been struggling with this task, I think I have got it working, but how do I remove the comma "," and the time "0:00:00" output from the result?

I only want it to display the number of days and the word days.

INPUT: 1989 6 21

DESIRED OUTPUT (at time/date of asking question!): 349 days

NOT: 349 days, 0:00:00

Hope that is clear and thanks in advance!

--

So far I have:

import datetime


today = datetime.date.today()

user_birth_year = int(input("Enter year of birth i.e. 1989: "))
user_birth_month = int(input("Enter month of birth i.e. for June enter 6: "))
user_birth_day = int(input("Enter day of birth i.e. for 21st enter 21: "))

my_birthday = datetime.date(today.year, user_birth_month, user_birth_day)
if my_birthday == today:
    print("Happy Birthday!")
else:
    if my_birthday < today:
        my_birthday = my_birthday.replace(year=today.year + 1)
        days_until_birthday = my_birthday - today
        print(days_until_birthday)

    else:
        days_until_birthday = my_birthday - today
        print(days_until_birthday)
Brisco
  • 99
  • 9
  • 2
    Change `days_until_birthday` to `days_until_birthday.days`. Read more: https://stackoverflow.com/questions/25646200/python-convert-timedelta-to-int-in-a-dataframe – Ghoti Jul 07 '21 at 17:29

1 Answers1

1

This is included in any tutorial on datetime. If you want only the days, then access the days attribute.

else:
    days_until_birthday = my_birthday - today
    print(days_until_birthday.days, "days")
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Yes I have realised that I can access the attribute from the date object as you said. I did this and it works, I thought I had tried it before |: maybe I need a tea break :) – Brisco Jul 07 '21 at 17:37
  • 1
    Tea break is always a good idea before posting here. I took at least three breaks before each of my questions. :-) Since this is a problem solved directly by following the documentation, I suggest that you simply delete the question. – Prune Jul 07 '21 at 17:39