I get datetime
object from email message and then I try to compare it with datetime.now()
.
And then I see this error:
datetime.now() > datetime.strptime('Fri, 31 Jan 2020 09:59:34 +0000 (UTC)', "%a, %d %b %Y %H:%M:%S %z (%Z)"
TypeError: can't compare offset-naive and offset-aware datetimes
How to solve it?
In many cases, you don't want to have to convert any time zone information. To prevent this, just convert the datetime objects to floats on both sides of the comparator. Use the datetime.timestamp()
function.
I also suggest you simplify your date parsing with dateutil.parser.parse().
It's easier to read.
In your example, you might compare your datas like this:
compare_date = 'Fri, 31 Jan 2020 09:59:34 +0000 (UTC)'
datetime.now().timestamp() > parse(compare_date).timestamp()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With