How to check if dates in array are in range Last 365 days from today Python Selenium

Issue

I have a list of items issued last 365 days, last 90 days, etc.
I need to check if days are in this range: today – previous 365 days, today – last 90 days.

I created a method that sorts strings with dates, but I do not know how to check the range from the current day to the last 365 days.

I want to ensure the dates are correct since I have to check them daily.

   def sort_date_by_last_365_days(self):
    elements = self.driver.find_elements(By.XPATH, '//span[@class  ="date-content"]')
    empty_list = []
    for e in elements:
        print(e.text)
    for i in elements:
        empty_list.append(i.text)

    print(empty_list)
    data = pd.DataFrame({'Date': empty_list})
    print(data)
    data['Date'] = pd.to_datetime(data['Date'])
    data.sort_values(by='Date')
    print(data)
    return FrameworkResult(status=0)

The method gets all dates and sorts dates.

Solution

from datetime import datetime
from dateutil.relativedelta import relativedelta

date_to_check = datetime.strptime('2022-06-23', '%Y-%m-%d')
today = datetime.today()
print(date_to_check,today)
one_year_from_now = today - relativedelta(years=1)

if today >= date_to_check >= one_year_from_now:
    print('ys')

You can use datetime + relativedelta and check between a range like year from today and today?

Answered By – Arundeep Chohan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published