我有一个 python 脚本,我需要比较两个日期。我有一个日期列表作为 time.struct_time 对象,我需要将其与几个 datetime.date 对象进行比较。
如何将 datetime.date 对象转换为 time.struct_time 对象?或者我可以直接使用它们进行比较吗?
最佳答案
尝试使用 date.timetuple()
.来自 Python 文档:
Return a
time.struct_time
such as returned bytime.localtime()
. The hours, minutes and seconds are 0, and the DST flag is -1.d.timetuple()
is equivalent totime.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1))
, whereyday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1
is the day number within the current year starting with 1 for January 1st.
关于python - 如何将 datetime.date 对象转换为 time.struct_time 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5657308/