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

Similar Posts: