在开始和结束时间之间的值

时间:2013-01-28 08:05:30

标签: python

我有一个变量start_time和end_time以及另一个名为confirmed_time的值。我需要做一个匹配,确认__time在start_time和end_time之间。我怎么会发现。时间采用以下格式:

starting_time = "Jan 25 10:00:00"
ending_time   = "Jan 26 20:06:59"

confirmed_time也采用相同的格式。

任何帮助将不胜感激

6 个答案:

答案 0 :(得分:6)

您可以使用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 ...."


>>> 

答案 1 :(得分:1)

使用dateutil解析值并将它们转换为datetime。然后检查您的confirmed_time是否在所需的时间间隔内。

答案 2 :(得分:0)

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

检查将字符串转换为日期时间对象时可以使用的formats

答案 3 :(得分:0)

我建议您安装并使用dateutlil module,因为这样可以更轻松地解析日期。

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())

答案 4 :(得分:0)

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")

答案 5 :(得分:0)

这有效:

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使用的input format来处理多年(如评论中所示):

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))