简体   繁体   English

ValueError:日期超出月份python的范围

[英]ValueError: day is out of range for month python

I am writing code that lets users write down dates and times for things they have on. 我正在编写代码,使用户可以写下他们所拥有的东西的日期和时间。 It takes in the date on which it starts, the start time and finish time. 它包含开始日期,开始时间和结束时间。 It also allows the user to specify if they want it to carry over into multiple weeks (every Monday for a month for example) I am using a for loop to do this, and because of the different months having different days I obviously want (if the next Monday for example is in the next month) it to have the correct date. 它还允许用户指定是否要延续几个星期(例如每个月的每个星期一),我正在使用for循环来执行此操作,并且由于不同的月份我显然想要不同的日期(如果例如,下一个星期一是下个月)。

This is my code: 这是我的代码:

for i in range(0 , times):
    day = day
    month = month
    fulldateadd = datetime.date(year, month, day)
    day = day + 7
    if month == ( '01' or '03' or '05' or '07' or '10'or '12'):
        if day > 31:
            print(day)
            day = day - 31
            print(day)
            month = month + 1
        elif month == ( '04' or '06'or '09' or '11'):
            if day > 30:
                print(day)
                day = day - 30
                print(day)
                month = month + 1
        elif month == '02':
            if day > 29:
                print(day)
                day = day - 29
                print(day)
                month = month + 1

When running this and testing to see if it goes correctly into the new month I get the error: 在运行此程序并进行测试以查看它是否可以正确进入新的月份时,我收到错误消息:

  File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 73, in addtimeslot
fulldateadd = datetime.date(year, month, day)
ValueError: day is out of range for month

Where have I gone wrong? 我哪里出问题了?

It's hard to be completely accurate without seeing some of the previous code (for example, where do day , month , year , and times come from?), but here's how you might be able to use timedelta in your code: 如果没有看到一些先前的代码(例如, daymonthyeartimes从哪里来?),很难完全准确,但是这是您可以在代码中使用timedelta的方式:

fulldateadd = datetime.date(year, month, day)
for i in range(times):
    fulldateadd = fulldateadd + datetime.timedelta(7)

A timedelta instance represents a period of time, rather than a specific absolute time. timedelta实例代表一个时间段,而不是特定的绝对时间。 By default, a single integer passed to the constructor represents a number of days. 默认情况下,传递给构造函数的单个整数表示天数。 So timedelta(7) gives you an object that represents 7 days. 因此timedelta(7)给您一个代表7天的对象。

timedelta instances can then be used with datetime or date instances using basic arithmetic. 然后可以使用基本算术将timedelta实例与datetimedate实例一起使用。 For example, date(2016, 12, 31) + timedelta(1) would give you date(2017, 1, 1) without you needing to do anything special. 例如, date(2016, 12, 31) + timedelta(1)将为您提供date(2017, 1, 1) date(2016, 12, 31) + timedelta(1) date(2017, 1, 1)而您无需执行任何特殊操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM