When I use dateutil.parser
to parse an incomplete date that's missing the day, I get the day set to 10
for some reason:
from dateutil.parser import parse
>>> d1 = parse('2008 Apr 2')
>>> d1
datetime.datetime(2008, 4, 2, 0, 0)
>>> d2 = parse('2014 Apr')
>>> d2
datetime.datetime(2014, 4, 10, 0, 0)
Is there a way to changing this so that the day gets automatically set to 1
instead for such incomplete cases?
You can pass default
keyword argument. If the default
is specified, parser will replace default's part with parsed date:
>>> import datetime
>>> from dateutil.parser import parse
>>>
>>> print parse('2014 Apr', default=datetime.datetime(2015, 1, 1))
2014-04-01 00:00:00
According to dateutil.parser.parse
documentation:
default
– The default datetime object, if this is a datetime object and notNone
, elements specified intimestr
replace elements in the default object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With