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

标签 python datetime time

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

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

最佳答案

尝试使用 date.timetuple() .来自 Python 文档:

Return a time.struct_time 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 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5657308/

相关文章:

python - 函数从数组的行中找到大于零的最小数并存储到列表中

python - 执行 `Solving environment: failed` 时遇到 `conda env` 错误

python - 在类之间传递 tkinter Canvas ,而不从父级内部调用子级

c# - 如何使用 DateTime.Now 函数

python - 如何正确测量jupyter中单元格的执行时间?

python - Django is_valid() 不适用于 modelformset_factory

c# - 仅搜索日期

c# - 奇怪的日期计算和测试

excel - 将文本日期/时间转换为 Excel 中的实际日期时间

c - 在内核空间上实现 timecounter_init (clocksource.h) 会出现无效的 ISA 状态错误