如何在 Python 中从日期中减去 Python timedelta?
pythonserver side programmingprogramming scripts更新于 2026/2/2 18:20:17
您可以使用 timedelta 对象从 Python 日期中减去一天。您需要创建一个 timedelta 对象,其中包含要减去的时间量。然后从日期中减去它。
示例
from datetime import datetime from datetime import timedelta today = datetime.today() yesterday = today - timedelta(days=1) print(today) print() print(yesterday)
输出
将给出输出 −
2017-12-29 12:28:06.531791 2017-12-28 12:28:06.531791
您还可以使用 timedelta 对象以相同的方式从日期中减去年、月、小时等。
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

