我有一个变量 start_time 和 end_time 以及另一个名为 confirm_time 的值。我需要做一个确认时间在 start_time 和 end_time 之间的匹配。我将如何发现。时间格式如下:
starting_time = "Jan 25 10:00:00"
ending_time = "Jan 26 20:06:59"
Confirmed_time 也是相同的格式。
任何帮助将不胜感激
我有一个变量 start_time 和 end_time 以及另一个名为 confirm_time 的值。我需要做一个确认时间在 start_time 和 end_time 之间的匹配。我将如何发现。时间格式如下:
starting_time = "Jan 25 10:00:00"
ending_time = "Jan 26 20:06:59"
Confirmed_time 也是相同的格式。
任何帮助将不胜感激
您可以使用datetime.strptime将日期和时间解析为datetime对象并进行比较。请注意,重要的是每个时间的格式保持相同
>>> starting_time = "Jan 25 10:00:00"
>>> ending_time = "Jan 26 20:06:59"
>>> confirmed_time = "Jan 25 11:11:11"
>>> starting_time_dt = datetime.strptime(starting_time,"%b %d %H:%M:%S")
>>> ending_time_dt = datetime.strptime(ending_time,"%b %d %H:%M:%S")
>>> confirmed_time_dt = datetime.strptime(confirmed_time,"%b %d %H:%M:%S")
>>> if starting_time_dt <= confirmed_time_dt <= ending_time_dt:
print "Within ...."
Within ....
>>> confirmed_time = "Jan 24 11:11:11"
>>> confirmed_time_dt = datetime.strptime(confirmed_time,"%b %d %H:%M:%S")
>>> if starting_time_dt <= confirmed_time_dt <= ending_time_dt:
print "Within ...."
>>>
使用 dateutil 解析值并将它们转换为日期时间。然后检查您的确认时间是否在所需的时间间隔内。
datetime 模块会有所帮助
In [41]: starting_time = "Jan 25 10:00:00"
In [42]: datetime.datetime.strptime(starting_time, "%b %d %H:%M:%S")
Out[42]: datetime.datetime(1900, 1, 25, 10, 0)
In [43]: s = datetime.datetime.strptime(starting_time, "%b %d %H:%M:%S")
In [44]: ending_time = "Jan 26 20:06:59"
In [45]: e = datetime.datetime.strptime(ending_time, "%b %d %H:%M:%S")
In [46]: e
Out[46]: datetime.datetime(1900, 1, 26, 20, 6, 59)
In [47]: confirm_time = "Jan 26 12:00:00"
In [48]: c = datetime.datetime.strptime(confirm_time, "%b %d %H:%M:%S")
In [49]: if s < c < e:
....: print True
....:
True
检查这些可以在将字符串转换为日期时间对象时使用的格式
我建议您安装并使用dateutlil 模块,因为它使解析日期更加容易。
from dateutil import parser
s_time = "Jan 25 10:00:00"
e_time = "Jan 26 20:06:59"
f_time = "Jan 25 12:10:00"
def example(conf, start, end):
p = parser.parse
return p(start) < p(conf) < p(end)
print example(f_time, s_time, e_time)
#output: True
parser.parse
可以处理大多数甚至晦涩的格式:
In[21]: parser.parse("Sat Jan 26 00:31:17 UTC 2013")
Out[21]: datetime.datetime(2013, 1, 26, 0, 31, 17, tzinfo=tzutc())
def isDateBetween(starting_time, ending_time, confirmed_time):
pattern = '%b %d %H:%M:%S'
startDate = datetime.datetime.strptime(starting_time, pattern)
endDate = datetime.datetime.strptime(ending_time, pattern)
confirmedDate = datetime.datetime.strptime(confirmed_time, pattern)
return startDate <= confirmedDate <= endDate
print isDateBetween("Jan 25 10:00:00", "Jan 26 20:06:59", "Jan 27 12:20:00")
这有效:
import datetime as dt
starting_time = "Jan 25 10:00:00"
ending_time = "Jan 26 20:06:59"
confirmed_time = "Jan 26 08:10:01"
fmt="%b %d %H:%M:%S"
print (dt.datetime.strptime(starting_time,fmt) <
dt.datetime.strptime(confirmed_time,fmt) <
dt.datetime.strptime(ending_time,fmt))
请注意,如果没有指定年份,则datetime
默认为 1900。然而,缺少年份有点模糊。如果:
starting_time = "Dec 26 10:00:00"
ending_time = "Jan 26 20:06:59" # the following year or the same year?
confirmed_time = "Jan 02 08:10:01"
编辑
您可以通过更改strptime 使用的输入格式来处理年份(如您的评论) :
import datetime as dt
starting_time = "Dec 25 10:00:00 UTC 2012"
ending_time = "Jan 26 20:06:59 UTC 2013"
confirmed_time = "Jan 02 08:10:01 UTC 2013"
fmt="%b %d %H:%M:%S UTC %Y"
print (dt.datetime.strptime(starting_time,fmt) <
dt.datetime.strptime(confirmed_time,fmt) <
dt.datetime.strptime(ending_time,fmt))