0

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.

Date_from is starting date e.g 01-04-2021

date_to is end date e.g 05-04-2021

check_in is the date I want to check e.g 03-04-2021

Python code:

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.

TypeError: '<' not supported between instances of 'generator' and 'generator'

Can anyone guide me on how can I check it?

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • 1
    You are doing WAY too much in a single line of code. Break it down into smaller pieces. It will probably help if you describe **in words** the steps you want to take to solve the problem. Reduce this to the most basic terms you can. What you have here is NOT comparing two dates. – Code-Apprentice Apr 01 '21 at 15:01
  • @Code-Apprentice can you explain me? I am not clear. It would be great. Thanks – Syed Arsalan Hussain Apr 01 '21 at 15:13
  • Please explain more clearly what you are trying to do. Instead of worry about technical details like "loops", explain in words the actual problem. It looks like you have a list of holidays and a "check in" date. What is a holiday? I mean in the United States a holiday is usually only one day, but you seem to have a "start date" and "end date" for each one here. And what are you trying to do with the holidays and the checkin date? What is the actual problem you are trying to solve? – Code-Apprentice Apr 01 '21 at 15:25

3 Answers3

1

You could use any to test if any of the elements of holydays matches:

any(d.date_from.date()<=check_in.date()<d.date_to.date() for d in holidays)
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I just want to point out for the OP that this also compares the checkin date with each holiday individually. Also, the use case for `any()` depends on if you want to know whether or not any of the tests succeed. If you want to do something with the specific holiday that matches, then you will need something more along the lines of what I posted in my answer. (By "you" here, I mean the OP) – Code-Apprentice Apr 01 '21 at 16:36
0

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.

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

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.
  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. Then, with your dates as date objects, you can just compare the dates as shown in the answer you linked above.

Note that that last else statement is for the for loop.

Let me know if this works for you.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19