-3

I wrote a Python function that prints the number of days remaining in birthday of a user - the value of which is entered by the user. The code is as follows:

"""
Created on Thu Feb 20 16:01:33 2020
@author: hussain.ali

"""
    import datetime as dt
    import pytz
    def days_to_birthday():
        a = (input('Enter your birthday in YYYY, MM, DD format with the year being the current year:'))
        td = dt.datetime.today()
        #td2= td.replace(hour=0, minute =0, second =0, microsecond =0)
        birthday = dt.datetime.strptime(a, '%Y,%m,%d')
        days_to_birthday = birthday - td
        print("There are", days_to_birthday, ' remaining until your next birthday!')

    days_to_birthday()

This script or code works well except that it gives the number of days plus hours as well as minutes, seconds and even microseconds remaining until the next birthday.

The output seems like:

Enter your birthday in YYYY, MM, DD format with the year being the current year:2020,3,7
There are 15 days, 6:11:07.020133  remaining until your next birthday!

I want either only the days remaining to be displayed in the output OR the output as: There are 15 days, 6 hours, 11minutes, 07seconds, and 020133 microseconds remaining until your next birthday!

What one needs to do to attain the desired output? Please advise.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
HusaynAly
  • 29
  • 6
  • @furas, please advice!! – HusaynAly Feb 20 '20 at 12:30
  • 1
    change your print to `print("There are", days_to_birthday.days, 'days remaining until your next birthday!')` if you only want the days to be printed – luigigi Feb 20 '20 at 12:31
  • Does this answer your question? [How to split a string into a list?](https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list) – Jared Smith Feb 20 '20 at 12:31
  • Also, [this](https://docs.python.org/3.8/library/stdtypes.html#str.split). – Jared Smith Feb 20 '20 at 12:33
  • 1
    Why would he be spitting a string? Just use luigigi's suggestion to get the number of days from the `timedelta` object. – Seb Feb 20 '20 at 12:33

2 Answers2

1

change your print statement to this code below.

print("There are", days_to_birthday.days, 'days remaining until your next birthday!')
  • @Janine Corro, it worked, but how can everything else(hours, minutes, seconds and microseconds) can be displayed in the o/p? – HusaynAly Feb 20 '20 at 12:51
1

timedelta doesn't have strftime() to format it so you can do one of two things:

  1. get total_seconds() and calculate all values using divmod() or using // and %

    total = days_to_birthday.seconds
    rest, seconds = divmod(total, 60)
    hours, minutes = divmod(rest, 60)
    
    days = days_to_birthday.days
    microseconds = days_to_birthday.microseconds
    
    print('{} days {:02} hrs {:02} mins {:02} secs {:06} microseconds'.format(days, hours, minutes, seconds, microseconds))
    
  2. get string 15 days, 6:11:07.020133, split it and use parts to create new string

    days = days_to_birthday.days
    parts = str(days_to_birthday).split(', ')[1].replace('.', ':').split(':')
    
    print('{} days {} hrs {} mins {} secs {} microseconds'.format(days, *parts))
    

import datetime as dt
import pytz

#a = input('Enter your birthday in YYYY,MM,DD format with the year being the current year:')
a = '2020,06,01'
print('date:', a)
td = dt.datetime.today()

birthday = dt.datetime.strptime(a, '%Y,%m,%d')
days_to_birthday = birthday - td

print(days_to_birthday)

total = days_to_birthday.seconds
rest, seconds = divmod(total, 60)
hours, minutes = divmod(rest, 60)

days = days_to_birthday.days
microseconds = days_to_birthday.microseconds

print('{} days {:02} hrs {:02} mins {:02} secs {:06} microseconds'.format(days, hours, minutes, seconds, microseconds))

days = days_to_birthday.days
parts = str(days_to_birthday).split(', ')[1].replace('.', ':').split(':')

print('{} days {} hrs {} mins {} secs {} microseconds'.format(days, *parts))

Result

date: 2020,06,01
93 days, 21:35:15.056351
93 days 21 hrs 35 mins 15 secs 056351 microseconds
93 days 21 hrs 35 mins 15 secs 056351 microseconds
furas
  • 134,197
  • 12
  • 106
  • 148