[英]Comparing time object with literal string in Python
I am trying to compare the current time with a value given as text string. 我正在尝试将当前时间与以文本字符串形式给出的值进行比较。 I don't know how to bring things to a common denominator, because I feel that the time obtained is not quite the same as a simple text string, even if it looks so when printing. 我不知道如何将事物带到一个共同的分母上,因为我觉得获得的时间与简单的文本字符串不太一样,即使在打印时看起来也是如此。
Test code: 测试代码:
import time
import datetime
def GetTimeAndWeekday(data):
datetime_now = datetime.datetime.now()
if data == "hms":
time_now = datetime.time(datetime_now.hour, datetime_now.minute, datetime_now.second)
return time_now
elif data == "hm":
time_now = datetime.time(datetime_now.hour, datetime_now.minute)
return time_now
return datetime_now
data = "hm"
found = GetTimeAndWeekday(data)
print found # for "hm" this gives current time in HH:MM:00 format, say 05:29:00
# I want to do something like this logic:
if found == "05:29:00":
print "the same"
else:
print "not the same"
How can I do that logic ? 我该怎么做呢?
Use strftime() 使用strftime()
if found.strftime('%I:%M:%S') == "05:29:00":
print 'the same'
else:
print 'not the same'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.