0

I am trying to compare two dates - today and date from db. Db date format is 2021-10-04T21:00:00Z and today date is 2023-08-11 09:06:10 (%Y-%m-%d %H:%M:%S).

How to compare these dates?

I tried this:

from datetime import datetime
from time import strftime, gmtime

today_time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
date_format = '%Y-%m-%d %H:%M:%S'

due_date = datetime.strptime(date_from_db, date_format)
if due_date < today_time:
    # ...

And I get following exception:

ValueError: time data '2021-10-04T21:00:00Z' does not match format '%Y-%m-%d %H:%M:%S'

How to resolve this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • to parse an ISO 8601 / RFC 3339 datetime string like "2021-10-04T21:00:00Z", use directive "%Y-%m-%d %H:%M:%S%z" or [fromisoformat](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat) in Python 3.11+ – FObersteiner Aug 11 '23 at 09:17
  • also https://stackoverflow.com/questions/31066805/python-how-can-i-convert-string-to-datetime-without-knowing-the-format – tevemadar Aug 11 '23 at 09:18

0 Answers0