Reputation: 22011
(Pdb) arrow.get('2016-01-01')
<Arrow [2016-01-01T00:00:00+00:00]>
(Pdb) arrow.get('20160101')
<Arrow [1970-08-22T08:01:41+00:00]>
So, I want my function to be able to use arrow
library in python to parse date strings. However, as can be seen from above code, it gives different results based on whether -
is present in the date string or not. How can I modify it so that it gives same results for both?
Upvotes: 0
Views: 203
Reputation: 21619
Simply provide a format string for the second object
arrow.get('20160101', 'YYYYMMDD')
The library probably defaults to the iso standard date format, but if you arent using that, you need to tell it how to interpret the string.
In the REPL
>>> arrow.get('2016-01-01')
<Arrow [2016-01-01T00:00:00+00:00]>
>>> arrow.get('20160101', 'YYYYMMDD')
<Arrow [2016-01-01T00:00:00+00:00]>
To use common call when the dashes are present or not, you could strip out the dashes
import arrow
vals = ['2016-01-01', '20160101']
for v in vals:
d = v.replace('-', '')
print(arrow.get(d, 'YYYYMMDD'))
Upvotes: 2