你们对此有什么解决方案吗?
from datetime import datetime
a = datetime(2012,9,23)
print(a + timedelta(days = 10))
它出现了日期时间未定义
a = datetime(2012,9,23)
NameError: name 'datetime' is not defined
答案 0 :(得分:0)
您只导入了datetime
但未导入timedelta
,因此您可以导入模块并引用其中的类
>>> import datetime
>>> a = datetime.datetime(2012,9,23)
>>> print(a + datetime.timedelta(days = 10))
2012-10-03 00:00:00
或者导入要明确使用的两个类
>>> from datetime import datetime, timedelta
>>> a = datetime(2012,9,23)
>>> print(a + timedelta(days = 10))
2012-10-03 00:00:00
答案 1 :(得分:-1)
您还需要导入timedelta
才能执行此任务。
from datetime import datetime, timedelta
a = datetime(2012,9,23)
print(a + timedelta(days = 10))
答案 2 :(得分:-1)
您忘记导入timedelta,以下代码应该有效。
from datetime import datetime, timedelta
a = datetime(2012, 9, 23)
print(a + timedelta(days=10))