How to initialize datetime 0000-00-00 00:00:00 in Python?

Viewed 24372

I've been trying to use the datetime library in python to create a datetime object with this value: '0000-00-00 00:00:00'. However, I keep getting an error that the year is out of range. How can I properly initialize this?

3 Answers

In datetime, the constant for MINYEAR is 1:

datetime.MINYEAR

The smallest year number allowed in a date or datetime object. MINYEAR is 1.

To initialize the earliest possible date you would do:

import datetime

earliest_date = datetime.datetime.min 
# OR
earliest_date = datetime.datetime(1,1,1,0,0)

print earliest_date.isoformat()

Which outputs:

0001-01-01T00:00:00

"How to initialize datetime 0000-00-00 00:00:00 in Python?"

No allowed, see manual datetime.

Alternative solution to the question is the minimum datetime:

>>> datetime.datetime.combine(datetime.date.min, datetime.time.min)
datetime.datetime(1, 1, 1, 0, 0)

found while reading: https://docs.python.org/3.7/library/datetime.html#module-datetime

Related