-1

I used regex to find these dates in a string

matches = ['10 October 2019', '20 October 2019', '10 October 2019', '25 October 2019']

matches[0] and matches[2] are dates that a task was assigned, matches[1] and matches[3] are the due dates for the task. I need to check if the tasks are overdue, so I need to check if matches[1] and matches[3] are before today's date

This is what I have tried

index = 0

    for random_value in range(0, len(matches)/2):

        assert(matches[index]> date.today())
        index += 2

This is the error message I am getting

TypeError: '>' not supported between instances of 'str' and 'datetime.date'

How do I convert the matches[index] into a format to be compared with the current date?

  • If the format is fixed, `strptime` might work (`%d %B %Y` seems to be the format). This converts the string into a datetime, from which you can get the `date()` and compare to the reference. If the format is not fixed, you may need to use `dateutil.parser` instead as it can do "fuzzy parsing" (try multiple ways). – Masklinn Nov 25 '22 at 10:36
  • That aside, you should learn how `range` works and what it can do, because manually incrementing an `index` is entirely unnecessary. Also the result of a range is not random, idiomatically if you don't care about the value (because you just want n iterations) you'd use `_` e.g. `for _ in range(n):` – Masklinn Nov 25 '22 at 10:37
  • Finally `assert` is not a function, it's a statement, and writing it up as a function is a common cause for errors, don't do that. Assertions also seem like the wrong tool for the job here (catching assertion errors is way too generic), but it's not clear what the caller / surrounding code should be. – Masklinn Nov 25 '22 at 10:40
  • Alright thank you, I'll look into all of this – theramannoodle Nov 25 '22 at 10:43

1 Answers1

0

You need to convert that string to actual date. as below code:

datetime.strptime('10 October 2019', '%d %B %Y') > datetime.today()
R. Baraiya
  • 1,490
  • 1
  • 4
  • 17