Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python date.min does not convert back. Why?

Tags:

python

sqlite

Consider this code:

datestring = date.min.strftime("%d-%m-%Y")
date = datetime.strptime(datestring,"%d-%m-%Y")

This raises the following error:

Traceback (most recent call last):   File "D:/Dev/foo.py", line 37, in <module>
    datestring = date.min.strftime("%d-%m-%Y") ValueError: year=1 is before 1900; the datetime strftime() methods require year >= 1900

Why is the min date not 1900 then???

I'm using storing date.min in the init of a class as to set it to an empty date sortof. When I persist this to the sqlite database, I do not have a Null date this way.

I do this because when I read back an empty date from the db, it raises an error in dbapi2.py in the convertdate function

How do you guys solve this?

like image 880
Dennis Decoene Avatar asked Aug 09 '26 17:08

Dennis Decoene


1 Answers

You could stick the "ISO 8601 format: YYYY-MM-DD":

>>> from datetime import date, datetime
>>> date_str = date.min.isoformat()
>>> print date_str
0001-01-01
>>> date_obj = datetime.strptime(date_str,"%Y-%m-%d")
>>> print date_obj
0001-01-01 00:00:00

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!