Python dateutil.parser throws “ValueError: day is out of range for month”

后端 未结 2 1862
后悔当初
后悔当初 2021-01-04 19:55

I have a the following code that runs fine with input format like {Year}/{Month} except when it comes to 1994/02

Here is the sample code

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 20:30

    dtp.parse is filling in the missing day with the current date's day. You ran the code on 2013/01/29 and day 29 does not exist in February (i.e. 1994/02/29).

    Use this instead:

    dtp.parse('1994/01'+'/01')
    

    It will give consistent results (first day of month) regardless of when the code is executed.

提交回复
热议问题