Tag Archives: ValueError: day is out of range for month

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!

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

Date out of range

At that time, I used the datetime module to generate time format data, and I mistakenly transmitted the result caused by wrong parameters. So, a good check of the data can solve the problem

As follows:

# Convert string type data to time structured data
# Originally, I 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 shaking 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 not be added. At that time, it was wrong to add parameters to datetime. Datetime (2019,05,05,00,00)

It’s over