I need to subtract a timezone aware datetime.now() with datetime.min, but i keep getting this error TypeError: can't subtract offset-naive and offset-aware datetimes. Please help!
from datetime import datetime
from pytz import timezone
now = datetime.now(timezone('Europe/Dublin'))
result = now - datetime.min
You can convert it to UTC
:
In [1]: from datetime import datetime
In [2]: import pytz
In [3]: dt_min = datetime.min
In [4]: print(dt_min)
0001-01-01 00:00:00
In [5]: dt_min = dt_min.replace(tzinfo=pytz.UTC)
In [6]: print(dt_min)
0001-01-01 00:00:00+00:00
So your code would be:
from datetime import datetime
import pytz
now = datetime.now(pytz.timezone('Europe/Dublin'))
dt_min = datetime.min
result = now - dt_min.replace(tzinfo=pytz.UTC)
print(result)
output:
737202 days, 7:27:48.839353
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