如何在Python中找到n天前的日期?
问题描述:
晚上好,
我想写一个脚本,给我python几天的时间(我们叫d),它给我日期我们是d天前。
I would like to write a script where I give python a number of days (let s call it d) and it gives me the date we were d days ago.
我正在为模块日期时间苦苦挣扎:
I am struggling with the module datetime:
import datetime
tod = datetime.datetime.now()
d = timedelta(days = 50)
a = tod - h
Type Error : unsupported operand type for - : "datetime.timedelta" and
"datetime.datetime"
感谢您的帮助
答
您已将变量与变量混合在一起,可以减去timedelta d
from datetime.datetime.now()
没有问题:
You have mixed something up with your variables, you can subtract timedelta d
from datetime.datetime.now()
with no issue:
import datetime
tod = datetime.datetime.now()
d = datetime.timedelta(days = 50)
a = tod - d
print(a)
2014-12-13 22:45:01.743172