我试图比较几个日期:
date1 = request.args['date1']
date2 = request.args['date2']
datesList = readFile('list.csv')
time.strptime(date1, "%d/%m/%Y")
time.strptime(date2, "%d/%m/%Y")
between = 0
for line in datesList:
time.strptime(line[0], "%d/%m/%Y")
time.strptime(line[1], "%d/%m/%Y")
if line[0] <= date1 <= line[1]:
between = 1
if line[0] <= date2 <= line[1]:
between = 2
if date1 <= line[1] <= date2:
between = 5
return render_template('index.html', vari = between)
我使用GET方法请求两个输入日期,然后使用strptime
将它们转换为日期格式。 datesList.csv如下所示:
17/11/2015,25/11/2015
1/11/2015,23/11/2015
17/12/2015,6/01/2016
28/10/2015,5/11/2015
如果我输入日期为2016年5月4日和2016年5月6日的日期,它仍会返回between = 5
。我的代码有问题或我使用了错误的函数吗?
答案 0 :(得分:3)
重新影响你的价值观。
date1 = time.strptime(date1, "%d/%m/%Y")
date2 = time.strptime(date2, "%d/%m/%Y")
between = 0
for line in bookings:
date1 = time.strptime(line[0], "%d/%m/%Y")
date2 = time.strptime(line[1], "%d/%m/%Y")
if line[0] <= date1 <= line[1]:
between = 1
if line[0] <= date2 <= line[1]:
between = 2
if date1 <= line[1] <= date2:
between = 5
return render_template('index.html', vari = between)
将它们转换为时间戳,然后如果它仍然不起作用,您可以将它们作为数字进行比较。
time.mktime(datetime.datetime.strptime(date, format).timetuple())
你的循环经历了“预订”,我在你的代码中没有看到它。这是一个错误吗?