I came across this code to count how much days I have til my birthday.
I know the difference between 'from import' and 'import' however I am confused because based on the code below, the second function named 'days_remaining' I was able to write in the variable days = (current_year - now).days. But if I recall correctly, days is a method/attribute under timedelta and I shouldn't be able to use that if I wrote from datetime import datetime because I am specifically importing only datetime method in the module.
from datetime import datetime
import time
def get_user_birthday():
date_str = input("Enter your birth date in DD/MM/YYYY: ")
try:
birthday = datetime.strptime(date_str, "%d/%m/%Y")
except TypeError:
birthday = datetime.datetime(*(time.strptime(date_str, "%d/%m/%Y")[0:6]))
return birthday
def days_remaining(birth_date):
now = datetime.now()
current_year = datetime(now.year, birth_date.month, birth_date.day)
days = (current_year - now).days
if days < 0:
next_year = datetime(now.year+1, birth_date.month, birth_date.day)
days = (next_year - now).days
return days
birthday = get_user_birthday()
next_birthday = days_remaining(birthday)
print("Your birthday is coming in: ", next_birthday, " days")