Question: Write a program that asks the user to enter a number of seconds, and works as follows:
There are 60 seconds in a minute. If the number of seconds
Do it the other way around subtracting the secs as needed, and don't call it time; there's a package with that name:
def sec_to_time():
sec = int( input ('Enter the number of seconds:'.strip()) )
days = sec / 86400
sec -= 86400*days
hrs = sec / 3600
sec -= 3600*hrs
mins = sec / 60
sec -= 60*mins
print days, ':', hrs, ':', mins, ':', sec
#1 min = 60
#1 hour = 60 * 60 = 3600
#1 day = 60 * 60 * 24 = 86400
x=input('enter a positive integer: ')
t=int(x)
day= t//86400
hour= (t-(day*86400))//3600
minit= (t - ((day*86400) + (hour*3600)))//60
seconds= t - ((day*86400) + (hour*3600) + (minit*60))
print( day, 'days' , hour,' hours', minit, 'minutes',seconds,' seconds')
To convert seconds (as string) into datetime, this could also help. You get number of days and seconds. Seconds can be further converted into minutes and hours.
from datetime import datetime, timedelta
sec = timedelta(seconds=(int(input('Enter the number of seconds: '))))
time = str(sec)
seconds_in_day = 86400
seconds_in_hour = 3600
seconds_in_minute = 60
seconds = int(input("Enter a number of seconds: "))
days = seconds // seconds_in_day
seconds = seconds - (days * seconds_in_day)
hours = seconds // seconds_in_hour
seconds = seconds - (hours * seconds_in_hour)
minutes = seconds // seconds_in_minute
seconds = seconds - (minutes * seconds_in_minute)
print("{0:.0f} days, {1:.0f} hours, {2:.0f} minutes, {3:.0f} seconds.".format(
days, hours, minutes, seconds))
I'm not entirely sure if you want it, but I had a similar task and needed to remove a field if it is zero. For example, 86401 seconds would show "1 days, 1 seconds" instead of "1 days, 0 hours, 0 minutes, 1 seconds". THe following code does that.
def secondsToText(secs):
days = secs//86400
hours = (secs - days*86400)//3600
minutes = (secs - days*86400 - hours*3600)//60
seconds = secs - days*86400 - hours*3600 - minutes*60
result = ("{} days, ".format(days) if days else "") + \
("{} hours, ".format(hours) if hours else "") + \
("{} minutes, ".format(minutes) if minutes else "") + \
("{} seconds, ".format(seconds) if seconds else "")
return result
EDIT: a slightly better version that handles pluralization of words.
def secondsToText(secs):
days = secs//86400
hours = (secs - days*86400)//3600
minutes = (secs - days*86400 - hours*3600)//60
seconds = secs - days*86400 - hours*3600 - minutes*60
result = ("{0} day{1}, ".format(days, "s" if days!=1 else "") if days else "") + \
("{0} hour{1}, ".format(hours, "s" if hours!=1 else "") if hours else "") + \
("{0} minute{1}, ".format(minutes, "s" if minutes!=1 else "") if minutes else "") + \
("{0} second{1}, ".format(seconds, "s" if seconds!=1 else "") if seconds else "")
return result
EDIT2: created a gist that does that in several languages
This will convert n seconds into d days, h hours, m minutes, and s seconds.
from datetime import datetime, timedelta
def GetTime():
sec = timedelta(seconds=int(input('Enter the number of seconds: ')))
d = datetime(1,1,1) + sec
print("DAYS:HOURS:MIN:SEC")
print("%d:%d:%d:%d" % (d.day-1, d.hour, d.minute, d.second))