ValueError:day日期时间超出范围 [英] ValueError: day is out of range for month datetime
问题描述
文件C:\Users\dansi \AppData\Local\Programs\Python\Python36-32\gui test 3.py,第72行,addtimeslot
fulldateadd = datetime.date(年,月,日)
ValueError:日期超出月份的范围
相关代码的一部分:
for i in range(0,times):
fulldateadd = datetime.date(年,月,日)
cursor.execute('''INSERT INTO dates(Date,Name,Start,End)VALUES(?,?,?,?);'''((fulldateadd,name1,starttimehour,endtimehour))
day = day + 7
if day> 31:
month = month + 1
我试图增加月份天数超过31,但似乎不起作用。
>组件,然后创建一个新的不是一个好主意。主要是因为自己处理公历是不是那个愉快的IMHO,而datetime对象可以为你做。
在这一点上,一个更简单的方法是在循环中添加一个timedelta到你的datetime。例如,
>>>来自datetime import timedelta
>>>> times = 4
>>>> cur_date = datetime.date(2017,2,24)
>>> for _ in range(times):
print('today is {0},do something'.format(cur_date))
cur_date + = timedelta(days = 7)
今天是2017-02-24,做某事
今天是2017-03-03,做某事
今天是2017-03-10,做某事
今天是2017-03-17 ,做某事
这可以放在一个生成器中,这取决于你的用例。
>>>对于范围(次)中的x的(cur_date + timedelta(days = x * 7)),dt()为
print('today is {0},do something'.format(dt))
今天是2017-02-24,做某事
今天是2017-03-03,做某事
今天是2017-03-10,做某事
今天是2017-03 -17,做某事
或与Pandas pd.date_range
>>>将大熊猫导入为pd
>>> list(pd.date_range(start ='2017-02-24',periods = 4,freq ='7D'))
[Timestamp('2017-02-24 00:00:00' ,freq ='7D'),
时间戳('2017-03-03 00:00:00',频率='7D'),
时间戳('2017-03-10 00:00: 00',频率='7D'),
时间戳('2017-03-17 00:00:00',频率='7D')]
现在如果您尝试使用此方法,会发生什么情况?
>>>年,月,日= 2017,2,24
>>>对于我在范围(0,次):
天=天
fulldateadd = datetime.date(年,月,日)
打印('今天是{0},做某事'。 format(fulldateadd))
day = day + 7
if day> 31:
day = day - 31
month = month + 1
今天是2017-02-24,做某事
ValueErrorTraceback(最近的call last)
< ipython-input-255-7df608ebbf8e>在< module>()
1 for i in range(0,times):
2 day = day
----> 3 fulldateadd = datetime.date(年,月,日)
4 print('today is {0},do something'.format(fulldateadd))
5天=天+ 7
ValueError:day超出范围为
二月没有31天。 ..所以你必须包括一张支票与映射到每个月的天数。包括闰年的逻辑。
I have run into a problem with some code I have been writing. I take in four inputs ( day, month and year ) as a date, and times for how many times they want to repeat the task ( eg. every Monday for 3 weeks ). The code is great however if the weeks differ between months I get this error:
File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 72, in addtimeslot
fulldateadd = datetime.date(year, month, day)
ValueError: day is out of range for month
Part of code that is relevant:
for i in range(0 , times):
fulldateadd = datetime.date(year, month, day)
cursor.execute( '''INSERT INTO dates (Date, Name, Start, End) VALUES( ?,?,?,? );''', (fulldateadd , name1, starttimehour, endtimehour))
day = day + 7
if day > 31:
month = month + 1
I've tried to increment the month when the number of days are more than 31 however it doesn't seem to work.
There are several reasons why incrementing the components of a datetime and then creating a new one is not a good idea. Primarily because dealing with the Gregorian calendar yourself isn't that enjoyable IMHO, and datetime objects can do it for you.
On that note, a much more straightforward approach would be to add a timedelta to your datetime within the loop. For instance,
>>> from datetime import timedelta
>>> times = 4
>>> cur_date = datetime.date(2017, 2, 24)
>>> for _ in range(times):
print('today is {0}, do something'.format(cur_date))
cur_date += timedelta(days=7)
today is 2017-02-24, do something
today is 2017-03-03, do something
today is 2017-03-10, do something
today is 2017-03-17, do something
This could be placed in a generator as well, depending on your use case.
>>> for dt in (cur_date + timedelta(days=x*7) for x in range(times)):
print('today is {0}, do something'.format(dt))
today is 2017-02-24, do something
today is 2017-03-03, do something
today is 2017-03-10, do something
today is 2017-03-17, do something
or with Pandas pd.date_range
>>> import pandas as pd
>>> list(pd.date_range(start='2017-02-24', periods=4, freq='7D'))
[Timestamp('2017-02-24 00:00:00', freq='7D'),
Timestamp('2017-03-03 00:00:00', freq='7D'),
Timestamp('2017-03-10 00:00:00', freq='7D'),
Timestamp('2017-03-17 00:00:00', freq='7D')]
Now what would happen if you attempted this example with your approach?
>>> year, month, day = 2017, 2, 24
>>> for i in range(0 , times):
day = day
fulldateadd = datetime.date(year, month, day)
print('today is {0}, do something'.format(fulldateadd))
day = day + 7
if day > 31:
day = day - 31
month = month + 1
today is 2017-02-24, do something
ValueErrorTraceback (most recent call last)
<ipython-input-255-7df608ebbf8e> in <module>()
1 for i in range(0 , times):
2 day = day
----> 3 fulldateadd = datetime.date(year, month, day)
4 print('today is {0}, do something'.format(fulldateadd))
5 day = day + 7
ValueError: day is out of range for month
February doesn't have 31 days... so you would have to include a check with a mapping to the number of days in each month. Including logic for leap years.
这篇关于ValueError:day日期时间超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!