Refer to official documentation:
datetime — Basic date and time types — Python 3.12.4 documentation
1)时区问题
Date and time objects are categorized as "aware" and "naive", as the former include time-zone information while the latter not.
Determining if an object is aware or naive:
1) date: always naive
2) datetime: if aware, datetime.tzinfo != None & datetime.tzinfo.utcoffset(datetime) != None
3) time: if aware, time.tzinfo != None & time.tzinfo.utcoffset(time) != None
2)两个时间的差值问题
Class datetime.timedelta:
A duration expressing the difference between two datetime or date instances to microsecond resolution. distinction between aware and naive doesn't apply to timedelta objects. When create a objects of timedelta type, week, hour, minute and millisecond are optional and default to 0, but will be converted to days, seconds and microseconds, which are stored internally.
deltatime to hours:
total_hours = int(delta.total_seconds() / 3600)
compare to a definite deltatime:
zday = datetime.timedelta(days=0, seconds=0, microseconds=0)
aday = datetime.timedelta(days=1, seconds=0, microseconds=0)
3)字符串识别问题
字符串识别为日期:
日期转换为字符串:
try:
dif=img_date-ope_t
except:
try:
ope_t=datetime.strptime(ope_t,'%Y-%m-%d %H:%M:%S')
dif=img_date-ope_t
except:
ope_t=datetime.strptime(in_date[idx],'%Y-%m-%d %H:%M:%S')
dif=img_date-ope_t
nifti_post_ope_period.append(dif.days)
Refer to a note about timedelta transformation:
Python 如何获取 timedelta 在 Python 中的总小时数和分钟数|极客教程 (geek-docs.com)