Is there any way to use a strftime-like function for dates before 1900 in Python? Is there any way to use a strftime-like function for dates before 1900 in Python? python python

Is there any way to use a strftime-like function for dates before 1900 in Python?


isoformat works on datetime instances w/o limitation of range:

>>> import datetime>>> x=datetime.datetime(1865, 7, 2, 9, 30, 21)>>> x.isoformat()'1865-07-02T09:30:21'

If you need a different-format string it's not too hard to slice, dice and remix pieces of the string you get from isoformat, which is very consistent (YYYY-MM-DDTHH:MM:SS.mmmmmm, with the dot and following microseconds omitted if microseconds are zero).


The documentation seems pretty clear about this:

The exact range of years for which strftime() works also varies across platforms. Regardless of platform, years before 1900 cannot be used.

So there isn't going to be a solution that uses strftime(). Luckily, it's pretty straightforward to do this "by hand":

>>> "%02d-%02d-%02d %02d:%02d" % (d.year,d.month,d.day,d.hour,d.minute)'1899-01-01 00:00'


mxDateTime can handle arbitrary dates. Python's time and datetime modules use UNIX timestamps internally, that's why they have limited range.

In [5]: mx.DateTime.DateTime(1899)Out[5]: <mx.DateTime.DateTime object for '1899-01-01 00:00:00.00' at 154a960>In [6]: DateTime.DateTime(1899).Format('%Y-%m-%d')Out[6]: 1899-01-01