Python TypeError: can't subtract offset-naive and offset-aware datetimes

August 10, 2019

You must make sure both datetime have timezone info, or timezone info of None.

import datetime
from dateutil import parser as dateutil_parser

today = datetime.datetime.now()
print(today.tzinfo) # None: no timezone info

date_str = '2019-08-10T00:00:00Z'
d = dateutil_parser.parse(date_str)
print(d.tzinfo) # tzutc(): this has utc timezone info
d = d.replace(tzinfo=None)  # remove timezone info

delta = today - d
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.