ValueError: day is out of range for month [How to Solve]

The date is out of range.

At that time, I used the datetime module to generate time format data, which was the result of misinformation of wrong parameters by hand. So, check the data well to solve the problem.

as follows:

# Convert string type data into time structure data 
# Originally wanted to write the following code 
import datetime

date_init = ' 2019-05-10 00:00:00 '

date_end = datetime.datetime(int(date_init.split( ' - ' )[0]), int(date_init.split( ' - ' )[1]),int(date_init.split( ' - ' )[2]. split( '  ' )[0].lstrip(' 0 ')), int(date_init.split( ' - ' )[2].split( '  ' )[1].split( ' : ' )[0]) , int(date_init.split( ' - ' )[2].split( '  ' )[1].split( ' : ' )[1]), int(date_init.split( ' - ')[2].split( '  ' )[1].split( ' : ' )[2 ]))

print date_end,type(date_end)

# Hand shake is wrong, as follows:

import datetime

date_init = ' 2019-05-10 00:00:00 '

date_end = datetime.datetime(int(date_init.split( ' - ' )[0]), int(date_init.split( ' - ' )[1]),int(date_init.split( ' - ' )[2]. split( '  ' )[0].lstrip(' 1 ')), int(date_init.split( ' - ' )[2].split( '  ' )[1].split( ' : ' )[0]) , int(date_init.split( ' - ' )[2].split( '  ' )[1].split( ' : ' )[1]), int(date_init.split( ' - ')[2].split( '  ' )[1].split( ' : ' )[2 ]))

print date_end,type(date_end)

Causes an error.

In fact, .lstrip(‘0’) can be omitted. At that time, it was considered wrong to add parameters to datetime.datetime(2019,05,05,00,00,00), so it was added.

end!

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *