python value out of range,ValueError:day日期时间超出范围

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)

in ()

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值