enter time-1 // eg 01:12
enter time-2 // eg 18:59
calculate: time-1 to time-2 / 12
// i.e time between 01:12 to 18:59 divided by 12如何在Python中做到这一点。我是一个初学者,所以我真的不知道从哪里开始。
编辑后补充说:我不想要计时器。time-1和time-2都由用户手动输入。
提前感谢您的帮助。
发布于 2009-12-27 13:06:59
内置datetime模块中的datetime和timedelta类就是您所需要的。
from datetime import datetime
# Parse the time strings
t1 = datetime.strptime('01:12','%H:%M')
t2 = datetime.strptime('18:59','%H:%M')
# Do the math, the result is a timedelta object
delta = (t2 - t1) / 12
print(delta.seconds)发布于 2009-12-27 13:07:30
最简单和最直接的可能是:
def getime(prom):
"""Prompt for input, return minutes since midnight"""
s = raw_input('Enter time-%s (hh:mm): ' % prom)
sh, sm = s.split(':')
return int(sm) + 60 * int(sh)
time1 = getime('1')
time2 = getime('2')
diff = time2 - time1
print "Difference: %d hours and %d minutes" % (diff//60, diff%60)例如,典型的运行可能是:
$ python ti.py
Enter time-1 (hh:mm): 01:12
Enter time-2 (hh:mm): 18:59
Difference: 17 hours and 47 minutes发布于 2009-12-27 12:55:16
这是一个计时器,用于对代码执行进行计时。也许你可以用它来做你想做的事。time()返回自1970-01-01 00:00:00以来的当前时间,单位为秒和微秒。
from time import time
t0 = time()
# do stuff that takes time
print time() - t0https://stackoverflow.com/questions/1965201
复制相似问题