简体   繁体   English

如何检查日期是否位于来自循环的两个日期之间

[英]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:然而,这假设了几件事:

  1. A string in the format 'dd-mm-yyyy' is what's being returned from running .date() on your objects. 'dd-mm-yyyy' 格式的字符串是在对象上运行.date()所返回的内容。
  2. You want the print statements only once, at the end, to see if the date fit between any of the holidays.您只希望打印语句最后一次,以查看日期是否适合任何假期。

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.

相关问题 我如何找到日期是否介于两个日期之间 - How can i find if the date lies between two dates 获取日期在表中两个日期之间的对象 - Get objects in which today date lies between two dates in table 如何检查日期是否在python中的两个日期之间 - How to check if date is between two dates in python 如何使用熊猫检查日期列中的日期是否在不同列中的两个日期之间? - How do I check if a date in a date column is between two dates in different columns using pandas? Django检查帖子的日期是否在模型的两个日期之间 - Django check if date from post is between two dates from model 如何检查向量的元素是否位于 Numpy 中的两个向量之间? - How to check if elements of a vector lies between two vectors in Numpy? 无论给出阈值的顺序如何,我如何才能找到一个值是否介于两个特定阈值之间? - How can I find if a value lies between two specific thresholds regardless of the order in which the thresholds are given? 如何在Python中的日期排序列表中查找介于特定日期之间的日期? - How to find in a sorted list of dates where a specific date lies in between, in Python? 如何通过在 gitlab CICD 中设置环境变量(截止日期、起始日期和项目列表)从 CSV 文件中获取两个日期之间的记录? - How can I get records between two dates from CSV file by setting an environment variable (To date, From Date and List of project) in gitlab CICD? 如何检查两个日期是否介于代表开始日期和结束日期的数据框的两列之间 - How to check if two dates falls between two columns of dataframe representing start date and end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM