1

I need to scrape a date off of a web page and compare it to todays date and see if the scraped date is within 1 year of today and produce a True of False statement.

I spend about 4 hours yesterday trying to get this to work but kept getting errors from every example I could find on the internet when trying to use them in this implementation.

Eventually I got something that worked without errors but it produced the incorrect result.

Unfortunately I somehow managed to delete that script last night and now I'm back at square zero. Instead of beating my head against the wall can someone inform me of the correct way to do this?

I'm trying to do something like this

contract_end_date = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="lbl_CONTRACT_EXPIRES"]'))).text
(example, contract_end_date = "09/25/2022")
if contract_end_date <= todays_date_plus_1_year:
   return True
else:
   return False

Much appreciate the help

coinmaster
  • 133
  • 6
  • 1
    It would help if you provided an example value of `contract_end_date` as part of your question, since we can't run your code on its own and have it give us the same result that you're getting. The general shape of the solution, though, should involve using the `datetime` module to parse the string into a `datetime` object, which you can then easily do comparison operations on. It's not possible to actually write the code for you without seeing the format of the date since the date formatting is required for `datetime.datetime.strptime()`. – Samwise Jun 26 '22 at 16:29
  • [Subtracting Dates With Python](https://stackoverflow.com/q/12126318) or [How to subtract dates with python](https://stackoverflow.com/q/4863994) – 001 Jun 26 '22 at 16:30

2 Answers2

3

This should be doable with datetime.timedelta.

Just make sure that your contract_end_date is a datetime object.

Edit: Added the str to datetime conversion

from datetime import datetime, timedelta

def expires_within_a_year(date_str):
    """date_str format must be like this: 09/25/2022"""
    contract_end_date = datetime.strptime(date_str, "%m/%d/%Y")
    today = datetime.now()
    one_year = timedelta(days=365)
    one_year_later = today + one_year
    # true if within the next 365 days (so if today is 09/25/2022, enddate 09/24/2023 would return true, enddate 09/25/2023 would return false)
    return contract_end_date < one_year_later

ffrosch
  • 899
  • 6
  • 15
  • This will return `True` if `contract_end_date` is also more than one year from `one_year_later`. Example: `expires_within_a_year(datetime(2000, 6, 26))` – 001 Jun 26 '22 at 16:42
  • Oh right, I reproduced the expression from the question. I'm going to fix it in my answer from `contract_end_date <= one_year_later` to `contract_end_date < one_year_later` – ffrosch Jun 26 '22 at 16:47
  • Thank you sir! You've done it! This means a lot to me, much appreciated :) – coinmaster Jun 26 '22 at 16:48
  • It's a little unclear what OP wants, but since they've accepted your answer, I guess it's ok. – 001 Jun 26 '22 at 16:52
  • @JohnnyMopp: True, it was a little ambigious :-) – ffrosch Jun 26 '22 at 16:55
  • @coinmaster Glad that I could help :-) – ffrosch Jun 26 '22 at 16:56
0

a little bit shorter:

from datetime import datetime

def expires_within_a_year(date_str):
    contract_end_date = datetime.strptime(date_str, "%m/%d/%Y")
    return (contract_end_date - datetime.now()).days < 365

expires_within_a_year('09/25/2022')  # True
SergFSM
  • 1,419
  • 1
  • 4
  • 7