为什么找不到 Python 日期时间时间增量? [英] Why is Python datetime time delta not found?

查看:38
本文介绍了为什么找不到 Python 日期时间时间增量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以 mmddyyyy 格式制作日期数组.日期将从当天开始,然后到未来两周.所以这一切都取决于开始日期.当我运行我的代码时,我收到一条错误消息:

I am trying to make an array of dates in mmddyyyy format. The dates will start on the current day and then go two weeks into the future. So it all depends on the starting date. When I run my code I get an error that states:

Traceback (most recent call last):
File "timeTest.py", line 8, in <module>
day = datetime.timedelta(days=i)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

我不确定为什么会发生这种情况,因为在网上搜索后,我注意到人们正在以这种方式使用timedelta".

I am not sure why this is happening because after searching online, I noticed that people are using the 'timedelta' in this way.

这是我的代码:

import time
from datetime import datetime, date, time, timedelta

dayDates = []
today = datetime.now()
dayDates.append(today.strftime("%m%d%Y"))
for i in range(0,14):
    day = today + datetime.timedelta(days=i)
    print day

推荐答案

import time
from datetime import datetime, date, time, timedelta

dayDates = []
today = datetime.now()
dayDates.append(today.strftime("%m%d%Y"))
for i in range(0,14):
    day = today + datetime.timedelta(days=i)
    print day

您收到的错误说,datetime 没有属性 timedelta.它发生了,因为您已经从 datetime 导入了特定的东西.为了访问 timedelta,现在您输入 timedelta 而不是 datetime.timedelta.

The error that you are getting says, that datetime has no attribute timedelta. It happens, because you have imported from datetime specific things. In order to access timedelta now you type timedelta instead of datetime.timedelta.

import time
from datetime import datetime, date, time, timedelta

dayDates = []
today = datetime.now()
dayDates.append(today.strftime("%m%d%Y"))
for i in range(0,14):
    day = today + timedelta(days=i)
    print day

这样,您的代码应该可以正常工作.另外,请密切注意错误消息并尝试仔细阅读它们.如果你足够专注,你往往可以根据自己的情况来梳理问题.

Like that, your code should work properly. Also, pay closer attention to the error messages and try to read them carefully. If you focus enough, you often can sort out the problem basing on them on your own.

这篇关于为什么找不到 Python 日期时间时间增量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆