python – 如何将datetime.date对象转换为time.struct_time对象?

我有一个python脚本,我需要比较两个日期.我有一个列表日期作为time.struct_time对象,我需要与一些datetime.date对象进行比较.

如何将datetime.date对象转换为time.struct_time对象?或者我可以直接使用它们进行比较?

最佳答案
尝试使用date.timetuple().从Python文档:

Return a 07001 such as
returned by time.localtime(). The
hours, minutes and seconds are 0, and
the DST flag is -1. d.timetuple() is
equivalent to
time.struct_time((d.year, d.month,
d.day, 0, 0, 0, d.weekday(), yday,
-1))
, where yday = 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对象? - 代码日志