将格式HH:MM中的字符串与python中的时间进行比较 [英] Compare string in format HH:MM to time now in python
问题描述
我读过datetime文档,不了解一个优雅的表现我的比较(总菜鸟也没有帮助:))
感谢您阅读!
您可以使用 datetimes
的 strptime()
函数转换字符串到有效的 datetime
:
>>> d = datetime.datetime.strptime('15:30','%H:%M')
稍后将它与现在比较
的 time()
:
>>> dnow = datetime.datetime.now()#11:42 am here;)
>> dnow.time()< ; d.time()
True
您还可以阅读doc的 strftime()和strptime()行为解释了这些方法,并有一个很好的表恢复指令来解析日期。
I have a string of the format "HH:MM" and need to compare it to the time now in Python.
I did read through the datetime documentation but could not figure out an elegant to perform my comparison (being a total rookie does not help either:))
Thanks for reading this!
You can use datetimes
's strptime()
function to convert the string to a valid datetime
:
>>>d=datetime.datetime.strptime('15:30','%H:%M')
and later compare it to now
's time()
:
>>>dnow=datetime.datetime.now() #11:42 am here ;)
>>>dnow.time() < d.time()
True
You can read also doc's strftime() and strptime() Behavior which explained these methods and have a very good table resuming the directives to parse dates.
这篇关于将格式HH:MM中的字符串与python中的时间进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!