0

While I was trying to convert String into datetime , I encountered the following:

S='30-01-50'
datetime.datetime.str 
>>datetime.date(2050, 1, 30)

However

S='30-01-70'
datetime.datetime.strptime(S,"%d-%m-%y").date()
>>datetime.date(1970, 1, 30)

Why not 2070 ?. How does one perform arithmetic on such datetime values?

Ashish Pahwa
  • 115
  • 1
  • 8
  • Isn't the problem here that your string isn't unambiguous? Python supports years up to 9999, but 1970 is *what you should get* for only 70. Where is the input coming from, and why can't it include the full year? – jonrsharpe May 28 '16 at 10:14
  • I tried S='30-01-1970' , I threw the error : Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime data_string[found.end():]) ValueError: unconverted data remains: 70 – Ashish Pahwa May 28 '16 at 10:16
  • You didn't change the pattern from %y to %Y. Why? – jonrsharpe May 28 '16 at 10:16
  • Ooh! Din't know that , its working now . Thanks! .But technically how are they different from each other? – Ashish Pahwa May 28 '16 at 10:19

1 Answers1

0

For a quick reference, see this answer.

Quick answer: do not use two-digit years, use four-digit years and the %Y specifier.

Community
  • 1
  • 1
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237