[英]How can I check if the date lies between two dates coming from loop
I am stuck at a point I am trying to check if the date is in between two dates, Dates are coming from objects of python with for loop.我被困在一个点上,我试图检查日期是否在两个日期之间,日期来自带有 for 循环的 python 的对象。
Date_from is starting date eg 01-04-2021 Date_from 是开始日期,例如01-04-2021
date_to is end date eg 05-04-2021 date_to 是结束日期,例如05-04-2021
check_in is the date I want to check eg 03-04-2021 check_in 是我要检查的日期,例如03-04-2021
Python code: Python代码:
if check_in.date() in ((d.date_from.date() for d in holidays) < ((d.date_to.date() for d in holidays))):
print("date is in between")
else:
print("Not in between")
I have also tried this code but not successful.我也尝试过这段代码,但没有成功。
if check_in.date() in ((d.date_from.date() for d in holidays) or ((d.date_to.date() for d in holidays))):
print("date is in between")
else:
print("Not in between")
Here referring to this URL source to check date lies in between two dates I am getting this error.这里指的是这个URL 源来检查日期位于两个日期之间我收到此错误。
TypeError: '<' not supported between instances of 'generator' and 'generator' TypeError:“生成器”和“生成器”的实例之间不支持“<”
Can anyone guide me on how can I check it?谁能指导我如何检查它?
You could use any
to test if any of the elements of holydays
matches:您可以使用any
来测试holydays
的任何元素是否匹配:
any(d.date_from.date()<=check_in.date()<d.date_to.date() for d in holidays)
If I understand correctly, you are trying to do something like this:如果我理解正确,您正在尝试执行以下操作:
for each holiday
is the checkin date between the holiday start date and the holiday end date
Notice how I write this description entirely in words.请注意我是如何完全用文字来写这个描述的。 I use formatting similar to code because I'm thinking ahead a little, but the important part is describe the solution problem in words without worrying about python syntax at all.我使用类似于代码的格式,因为我有点超前思考,但重要的部分是用文字描述解决方案问题,而不用担心 python 语法。
Now we can translate these words into code:现在我们可以将这些词翻译成代码:
# for each holiday
for d in holidays:
# is the checkin date between the holiday start date and the holiday end date
if d.date_from < check_in < d.date_to:
print("in between")
else:
print("not in between")
I think your original attempt was trying to check all of the dates at once.我认为您最初的尝试是尝试一次检查所有日期。 Instead, you need to loop over the list of holidays and check one date at a time.相反,您需要遍历假期列表并一次检查一个日期。
I think this should do what you want it to:我认为这应该做你想做的事:
from datetime import date
def convert_date(string_date):
"""Takes string date in format dd-mm-yyyy and returns a list of integers in format [yyyy, mm, dd]"""
return reversed(list(map(int, string_date.split('-'))))
# Create check in date object
check_in_obj = date(*convert_date('03-04-2021'))
for d in holidays:
# set date obj for date_from and date_to
date_from_obj = date(*convert_date(d.date_from.date()))
date_to_obj = date(*convert_date(d.date_to.date()))
if date_from_obj <= check_in_obj <= date_to_obj:
print("date is in between")
break
else:
print("Not in between")
This assumes a couple of things however:然而,这假设了几件事:
.date()
on your objects. 'dd-mm-yyyy' 格式的字符串是在对象上运行.date()
所返回的内容。 So I made a small function for converting your string dates to a list of ints that can be used to make datetime.date
objects.所以我制作了一个小的 function 用于将您的字符串日期转换为可用于制作datetime.date
对象的整数列表。 Then, with your dates as date
objects, you can just compare the dates as shown in the answer you linked above.然后,将您的日期作为date
对象,您只需比较上面链接的答案中显示的日期即可。
Note that that last else
statement is for the for
loop.请注意,最后一个else
语句用于for
循环。
Let me know if this works for you.让我知道这是否适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.