Answer to Question #237249 in Python for kaavya

Question #237249

New Year Countdown

Given date-time D, write a program to print the time left for the next New Year.

Input

The input will be a single line containing the date-time in the string format similar to "Dec 30 2020 02:43 PM".

Output

The output should be a single line containing hours and minutes left for the next new year in the format similar to "33 hours 17 minutes".

Explanation

For example, if the given date-time is "Dec 30 2020 02:43 PM", the difference between "Dec 30 2020 02:43 PM" and "Jan 1 2021 00:00 AM" is 1 day 9 hours 17 minutes. The difference in hours and minutes is 1 * 24 + 9 hours and 17 minutes. So the output should be "33 hours 17 minutes".

Sample Input 1

Dec 30 2020 02:43 PM

Sample Output 1

33 hours 17 minutes

Sample Input 2

Jan 31 2020 04:43 AM

Sample Output 2

8059 hours 17 minutes


1
Expert's answer
2021-09-15T01:49:24-0400
import datetime as dt


def change_char(s, p, r): 
    return s[:p]+r+s[p+1:]




InputDate = "Dec 30 2020 02:43 PM"


InputDate = str(input("Enter Date (For example: Dec 30 2020 02:43 PM): "))
#print(InputDate)
if(InputDate[len(InputDate)-2]=='P' or InputDate[len(InputDate)-2]=='p'):
    t = int(str(InputDate[12])+str(InputDate[13]))+12
    InputDate = change_char(InputDate,12,str(t//10))
    InputDate = change_char(InputDate,13,str(t%10))
s = ""
for r in range(0,17):
    s = s+InputDate[r]
s = s+":00.0000"    


d = datetime.datetime.strptime(s, "%b %d %Y  %H:%M:%S.%f")
NewYear = dt.datetime(2021, 1, 1, 0, 0, 1)
differ = NewYear - d


days, seconds = differ.days, differ.seconds


hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
print("Time remaining in New Year (",NewYear,") = ",hours," hours and ",minutes," minutes")




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS