python - Python:如何比较两个小时
问题描述
我需要调用一个函数,确切地说是08:00, 18:00, 22:00
几个小时。我创建了一个示例来测试小时之间的比较。当当前时间达到其中之一时。放入一个 While 循环,认为这个例子可以用作秒表,但我认为我错了。比较这些值的最佳方法是什么?
currentH= dt.datetime.now().strftime("%H:%M:%S")
h = "16:15:10"
while True:
if(currentH==h):
print 'Ok'
print 'The current Hour is: '+h
解决方案
import datetime as dt
import time
currentH= dt.datetime.now().replace(microsecond=0).time()
hrs = ['00:02', '12:00']
for i in range(len(hrs)):
h = [int(x) for x in hrs[i].split(':')]
h = dt.datetime.now().replace(hour=h[0], minute=h[1], second=0,microsecond=0).time()
hrs[i] = h
while True:
currentH = dt.datetime.now().replace(microsecond=0).time()
print(currentH)
if currentH in hrs:
print('Time is now',currentH)
time.sleep(1)
推荐阅读
- javascript - 如何在 document.querySelectorAll('.square') 之后在正方形上闪烁背景图像
- wordpress - TTFB 很长第一次访问网站
- javascript - 为什么网站在托管后会导致大多数元素在进行微小更改时重新计算?
- correctness - 程序和后置条件中未知变量的Hoare三元组的有效性?
- javascript - Ace Editor:禁用光标所在的阴影线
- google-cloud-firestore - Firestore 中的多租户与单独的集合
- flutter - 使用tenserlite模型在颤振中进行图像分割
- c - 处理多个程序参数,但在 C 中只允许一个
- java - Java 8 可选:组合两个可能为空的对象
- javascript - 我将如何更改此代码以具有 5 x 4 网格?