String to date conversion problem - please help!

A forum for general discussion of the Python programming language.

String to date conversion problem - please help!

Postby alexmendelev » Sun Dec 01, 2013 2:24 pm

I am trying to convert a string in format of DD/MM/YYYY to date:

Code: Select all
>>> print datetime.datetime.strptime("17/02/2013", "%d/%m/%y")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: String was not recognized as a valid DateTime.
data=17/02/2013, fmt=%d/%m/%y, to: d'/'M'/'yy
Last edited by Mekire on Sun Dec 01, 2013 2:35 pm, edited 1 time in total.
Reason: First post lock. Code Tags.
alexmendelev
 
Posts: 2
Joined: Sun Dec 01, 2013 2:20 pm

Re: String to date conversion problem - please help!

Postby Mekire » Sun Dec 01, 2013 2:43 pm

Welcome to the forum. Please use codetags in future posts and read our "New Users Read This" thread.

A four digit year is %Y (note the capitalization).
A two digit year is %y (note the lower case)

Code: Select all
import datetime

print datetime.datetime.strptime("17/02/2013", "%d/%m/%Y")
Code: Select all
2013-02-17 00:00:00

-Mek
User avatar
Mekire
 
Posts: 984
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: String to date conversion problem - please help!

Postby alexmendelev » Sun Dec 01, 2013 3:56 pm

Thanks a lot!!!
alexmendelev
 
Posts: 2
Joined: Sun Dec 01, 2013 2:20 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests