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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With