Home > front end >  Converting date string to Datetime
Converting date string to Datetime

Time:09-15

I have a function which returns in YYYY-MM-DD i want to add 10 days to this date and check if today is the date. how can i do that.

def date_return:
    return YYYY-MM-DD

CodePudding user response:

  1. Use datetime.date.fromisoformat to turn YYYY-MM-DD into a datetime.date.
  2. Use datetime.timedelta(days=10) to add 10 days to that date.
  3. Compare it to datetime.date.today().
>>> import datetime
>>> yyyymmdd = "2022-09-04"
>>> print(
...   datetime.date.fromisoformat(yyyymmdd)   datetime.timedelta(days=10)
...   == datetime.date.today()
... )
True
  • Related