0

when trying to convert string object into date…

here s is a string..

======datetime_new.py============

s="05/30/2013:10:47:34"

mytime = time.strptime(s, "%m/%d/%Y %H:%M:%S")

print mytime

getting an error like:

ValueError: time data '05/30/2013:10:47:34' does not match format '%m/%d/%Y %H:%M:%S'

1 Answer 1

2

There's a colon between date and time parts, but a space in your format.

But time.strptime won't give you a datetime, it will give you a time.struct_time. If you want a datetime, use datetime.datetime.strptime.

In [1]: import time

In [2]: s="05/30/2013:10:47:34"

In [4]: mytime = time.strptime(s, "%m/%d/%Y:%H:%M:%S")

In [5]: mytime
Out[5]: time.struct_time(tm_year=2013, tm_mon=5, tm_mday=30, tm_hour=10, tm_min=47, tm_sec=34, tm_wday=3, tm_yday=150, tm_isdst=-1)

In [6]: import datetime

In [7]: datetime.datetime.strptime(s, "%m/%d/%Y:%H:%M:%S")
Out[7]: datetime.datetime(2013, 5, 30, 10, 47, 34)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.