Date Difference issue in Python 2.7.8

Chris Angelico rosuav at gmail.com
Fri Feb 20 04:46:01 EST 2015


On Fri, Feb 20, 2015 at 8:32 PM, Puruganti Ramesh
<puruganti.ramesh at gmail.com> wrote:
> import datetime as dt
> from datetime import datetime
> from datetime import datetime, timedelta, date

The first two lines here are achieving nothing - the third should be
all you need.

> dt_str='2014-5-11'
> dt_strq='2014-9-11'
>
> dt_datea = datetime.strptime(dt_str, '%Y-%m-%d').date()
> dt_dateb = datetime.strptime(dt_strq, '%Y-%m-%d').date()
> dt_diff = dt_dateb - dt_datea
> print dt_diff.days
>
> I am getting below excption
> Traceback (most recent call last):
> File "FunctionUpdate.py", line 204, in <module>
> dt_dateb = datetime.strptime(dt_strq, '%Y-%m-%d').date()
> File "/usr/local/lib/python2.7/_strptime.py", line 328, in _strptime
> data_string[found.end():])
> ValueError: unconverted data remains:

Just tried this on 2.7.3 on Debian Wheezy, and 2.7.8 on Debian Jessie,
and it gave me back a date difference of 123 days. Is this definitely
what's causing the issue? If so, can you cut it down to just the bare
essentials? For instance, does this also fail?

from datetime import datetime
datetime.strptime('2014-9-11', '%Y-%m-%d')

In theory, it should; my eyeballing of the code suggests no reason why
the two-line version would work when your larger one doesn't. But my
suspicion is that your larger code isn't triggering the problem
either; your traceback points to line 204, and I'd really like to see
a self-contained program that actually definitely has the problem.

ChrisA



More information about the Python-list mailing list