2

I need to print out “Your birthday is 31 March 2001 (a years, b days, c hours, d minutes and e seconds ago).”

I create input

birth_day = int(input("your birth day?"))

birth_month = int(input("your birth month?"))

birth_year = int(input("your birth year?"))

and I understand

print("your birthday is"+(birth_day)+(birth_month)+(birth_year)) to print out first sentence. but I faced problem with second one which is this part (a years, b days, c hours, d minutes and e seconds ago)

I guess I have to use “the epoch”

and use some of various just like below

year_sec=365*60*60*24
day_sec=60*60*24
hour_sec=60*60
min_sec=60

calculate how many seconds of the date since 1 January 1970 00:00:00 UTC:

import datetime, time
t = datetime.datetime(2001, 3, 31, 0, 0)
time.mktime(t.timetuple())

985960800.0

can anyone, could you solve my problem please?

Thank a lot

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
cali
  • 21
  • 2
  • Hey, answered your question below. Kept having typos and bugs so I edited it a couple times, but I **think** the code and explanation should be good now :) Leave a comment below my answer if I messed up anything or you want more clarification! – slow-but-steady Aug 14 '21 at 06:23
  • Why do you think "the epoch" is relevant to your problem? You are just trying to find the time elapsed between the current date and the user input date. – kaya3 Aug 14 '21 at 13:50
  • Does this answer your question? [Python calculating time difference, to give ‘years, months, days, hours, minutes and seconds’ in 1](https://stackoverflow.com/questions/25439279/python-calculating-time-difference-to-give-years-months-days-hours-minutes) – kaya3 Aug 14 '21 at 13:52

1 Answers1

0

EDIT: See this answer in the thread kaya3 mentioned above for a more consistently reliable way of doing the same thing. I'm leaving my original answer below since it's useful to understand how to think about the problem, but just be aware that my answer below might mess up in tricky situations due to the quirks of the Gregorian calendar, in particular:

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.


ORIGINAL ANSWER:

You can try using the time module:

import time
import datetime

def main(ask_for_hour_and_minute, convert_to_integers):
    year, month, day, hour, minute = ask_for_birthday_info(ask_for_hour_and_minute)
    calculate_time_since_birth(year, month, day, hour, minute, convert_to_integers)


def ask_for_birthday_info(ask_for_hour_and_minute):
    birthday_year = int(input('What year were you born in?\n'))
    birthday_month = int(input('What month were you born in?\n'))
    birthday_day = int(input('What day were you born on?\n'))
    if ask_for_hour_and_minute is True:
        birthday_hour = int(input('What hour were you born?\n'))
        birthday_minute = int(input('What minute were you born?\n'))
    else:
        birthday_hour = 0   # set to 0 as default
        birthday_minute = 0 # set to 0 as default
    return (birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute)

def calculate_time_since_birth(birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute, convert_to_integers):
    year   = 31557600 # seconds in a year
    day    = 86400    # seconds in a day
    hour   = 3600     # seconds in a hour
    minute = 60       # seconds in a minute
    # provide user info to datetime.datetime()
    birthdate = datetime.datetime(birthday_year, birthday_month, birthday_day, birthday_hour, birthday_minute)
    birthdate_tuple = time.mktime(birthdate.timetuple())
    # figure out how many seconds ago birth was
    seconds_since_birthday = time.time() - birthdate_tuple
    # start calculations
    years_ago   = seconds_since_birthday // year
    days_ago    = seconds_since_birthday // day % 365
    hours_ago   = seconds_since_birthday // hour % 24
    minutes_ago = seconds_since_birthday // minute % 60
    seconds_ago = seconds_since_birthday % minute
    # convert calculated values to integers if convert_to_integers is True
    if convert_to_integers is True:
        years_ago   = int(years_ago) 
        days_ago    = int(days_ago) 
        hours_ago   = int(hours_ago) 
        minutes_ago = int(minutes_ago) 
        seconds_ago = int(seconds_ago) 
    # print calculations
    print(f'Your birthday was {years_ago} years, {days_ago}, days, {hours_ago} hours, {minutes_ago} minutes, {seconds_ago} seconds ago.')

# to ask for just the year, month, and day
main(False, False)
# to ask for just the year, month, and day AND convert the answer to integer values
main(False, True)
# to ask for just the year, month, day, hour, and minute
main(True, False)
# to ask for just the year, month, day, hour, and minute AND convert the answer to integer values
main(True, True)

Tried to use descriptive variable names so the variables should make sense, but the operators might need some explaining:

10 // 3 # the // operator divides the numerator by the denominator and REMOVES the remainder, so answer is 3

10 % 3 # the % operator divides the numerator by the denominator and RETURNS the remainder, so the answer is 1

After understanding the operators, the rest of the code should make sense. For clarity, let's walk through it

  1. Create birthdate by asking user for their information in the ask_for_birthday_info() function
  2. Provide the information the user provided to the calculate_time_since_birth() function
  3. Convert birthdate to a tuple and store it in birthdate_tuple
  4. Figure out how many seconds have passed since the birthday and store it in seconds_since_birthday
  5. Figure out how many years have passed since the birthday by dividing seconds_since_birthday by the number of seconds in a year
  6. Figure out how many days have passed since the birthday by dividing seconds_since_birthday by the number of seconds in a day and keeping only the most recent 365 days (that's the % 365 in days_ago)
  7. Figure out how many hours have passed since the birthday by dividing seconds_since_birthday by the number of seconds in a hour and keeping only the most recent 24 hours (that's the % 24 in hours_ago)
  8. Figure out how many minutes have passed since the birthday by dividing seconds_since_birthday by the number of seconds in a minute and keeping only the most recent 60 minutes (that's the % 60 in minutes_ago)
  9. Figure out how many seconds have passed since the birthday by dividing seconds_since_birthday and keeping only the most recent 60 seconds (that's the % 60 in seconds_ago)

Then, we just need to print the results:

print(f'Your birthday was {years_ago} years, {days_ago}, days, {hours_ago} hours, {minutes_ago} minutes, {seconds_ago} seconds ago.')

# if you're using a version of python before 3.6, use something like
print('Your birthday was ' + str(years_ago) + ' years, ' + str(days_ago) + ' days, ' + str(hours_ago) + ' hours, ' + str(minutes_ago) + ' minutes, ' + str(seconds_ago) + ' seconds ago.')

Finally, you can add some error checking to make sure that the user enters valid information, so that if they say they were born in month 15 or month -2, your program would tell the user they provided an invalid answer. For example, you could do something like this AFTER getting the birthday information from the user, but BEFORE calling the calculate_time_since_birth() function:

if not (1 <= month <= 12):
    print('ERROR! You provided an invalid month!')
    return
if not (1 <= day <= 31):
    # note this isn't a robust check, if user provides February 30 or April 31, that should be an error - but this won't catch that
    # you'll need to make it more robust to catch those errors
    print('ERROR! You provided an invalid day!')
    return
if not (0 <= hour <= 23):
    print('ERROR! You provided an invalid hour!')
    return
if not (0 <= minute <= 59):
    print('ERROR! You provided an invalid minute!')
    return
if not (0 <= second <= 59):
    print('ERROR! You provided an invalid second!')
    return

slow-but-steady
  • 961
  • 9
  • 15
  • 1
    Thank you so much. I will try with this code. Actually this is my first time to learn python. really appreciate your help. :) – cali Aug 14 '21 at 08:50
  • and I have another question. from this code birthdate = datetime.datetime(2001, 3, 31, 0, 0) I want put input date instead of 2001,3,31,0,0 how to put input date in this code? do I just use birthdate=datetime.datetime(input1,input2,input3,0,0) ? – cali Aug 14 '21 at 08:52
  • Just edited the answer to ask the user for that information - I think that should work. To answer your question, I *think* you could ask for the information using `datetime.datetime(input1,input2,input3,0,0)`, but it would get messy, so I put all that in the `ask_for_birthday_info()` function in the updated answer – slow-but-steady Aug 15 '21 at 07:14