[Tutor] Issue in date difference in Python 2.7.8

Peter Otten __peter__ at web.de
Fri Feb 20 13:02:57 CET 2015


Puruganti Ramesh wrote:

> Hi Friends,
> 
> I have an issue in comparing dates in python 2.7.8
> I have written code as below but i am getting error
> Code is :
> import datetime as dt
> from datetime import datetime
> from datetime import datetime, timedelta, date
> 
> 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

No, the above does not trigger the ValueError.

> 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:
> 
> Kindly solve my issue

See my answer on comp.lang.python:

<https://mail.python.org/pipermail/python-list/2015-February/698959.html>




More information about the Tutor mailing list