两个datetime进行时差计算报错:
TypeError: can't subtract offset-naive and offset-aware datetimes
原来是两个相减的时间时区不一致
# -*- coding: utf-8 -*- from datetime import datetime import pytz now1 = datetime.now(tz=pytz.UTC) print(now1) # 零时区的时间 2021-06-11 04:11:39.588770+00:00 now2 = datetime.now() print(now2) # 东八区的时间 2021-06-11 12:12:17.431790 # print(now2 - now1) # TypeError: can't subtract offset-naive and offset-aware datetimes now3 = now1.replace(tzinfo=None) print(now3) # 去掉时区之后 2021-06-11 04:15:06.453159 print(now2 - now3) # 8:00:00.000061