0

I have the following format:

"Wed Jun 25 15:38:29 PDT 2014" and I would like to convert it to "2014-06-25 15:38:29", i.e. "%Y-%m-%d %H:%M:%S"

Code:

import time import datetime rawtime = "Wed Jun 25 15:38:29 PDT 2014" dt = time.strptime(rawtime, "%Y-%m-%d %H:%M:%S") print dt 

I get the error:

ValueError: time data 'Wed Jun 25 15:38:29 PDT 2014\n' does not match format '%Y-%m-%d %H:%M:%S' 
1
  • 2
    You have to specify the format the string is currently in for strptime, not the format you want it to be! Commented Aug 8, 2014 at 18:30

6 Answers 6

3

Simplest solution using dateutil package. It really is excellent at this.

from dateutil import parser print parser.parse("Wed Jun 25 15:38:29 PDT 2014") 

Output:

2014-06-25 15:38:29 
Sign up to request clarification or add additional context in comments.

Comments

1

First install pytz:

sudo easy_install --upgrade pytz 

Then

import datetime from pytz import timezone rawtime = "Wed Jun 25 15:38:29 PDT 2014" d = datetime.datetime.strptime(rawtime,'%a %b %d %H:%M:%S %Z %Y') print(d.strftime("%Y-%m-%d %H:%M:%S")) 

Should return:

2014-06-25 15:38:29 

Comments

0

Just get rid of the PDT part:

import time import datetime rawtime = "Wed Jun 25 15:38:29 PDT 2014" times = rawtime.split(' ') del times[4] dt = time.strptime(' '.join(times), '%a %b %d %H:%M:%S %Y') print time.strftime("%Y-%m-%d %H:%M:%S", dt) 

Should get you:

2014-06-25 15:38:29 

Comments

0

pip install python-dateutil:

In [56]: from dateutil import parser In [57]: parser.parse("Wed Jun 25 15:38:29 PDT 2014").strftime("%Y-%m-%d %H:%M:%S") Out[57]: '2014-06-25 15:38:29' 

Or manually:

In [58]: d = time.strptime("Wed Jun 25 15:38:29 PDT 2014","%a %b %d %H:%M:%S PDT %Y") In [59]: "{}-{:02}-{:02} {:02}:{:02}:{:02}".format(d.tm_year,d.tm_mon,d.tm_mday,d.tm_hour,d.tm_min,d.tm_sec) Out[59]: '2014-06-25 15:38:29' 

Comments

0

Usually you need to call strptime or use dateutil to parse date strings. However in this case, rawtime looks similar enough to a date as specified by the RFC 2822 (Internet Message Format) that it can parsed using the standard library function email.utils.parsedate:

import email.utils as EU import datetime as DT rawtime = "Wed Jun 25 15:38:29 PDT 2014" timetup = EU.parsedate(rawtime) # (2014, 6, 25, 15, 38, 29, 0, 1, -1) date = DT.datetime(*timetup[:6]) print(date) # 2014-06-25 15:38:29 

Comments

0

Hey So i found a solution for 95% of your problem.

import time import datetime rawtime = "Wed Jun 25 15:38:29 2014" dt = time.strptime("%a %b %d %H:%M:%S %Y",rawtime) print dt 

this will out time.struct_time(tm_year=2014, tm_mon=6, tm_mday=25, tm_hour=15, tm_min=38, tm_sec=29, tm_wday=2, tm_yday=176, tm_isdst=-1)

After this you just need to create a string and place them in the right order you want. The only thing I wasn't sure of how to do off the top of my head was the PDT part of it.

To save the dt as a variable you can do

x = str(dt.tm_year) + "-" + str(dt.tm_mon) + "-" + str(dt.tm_mday) + " " + str(dt.tm_hour) + ":" + str(dt.tm_min) + ":" + str(dt.tm_sec) print x 

this will output 2014-6-25 15:38:29

Good luck!

3 Comments

there is still the issue of the PDT thing: this what i got: (data_string, format)) ValueError: time data 'Wed Jun 25 15:38:29 PDT 2014\n' does not match format '%a %b %d %H:%M:%S %Y'
there is still the issue of the PDT thing: this what i got: (data_string, format)) ValueError: time data 'Wed Jun 25 15:38:29 PDT 2014\n' does not match format '%a %b %d %H:%M:%S %Y'::: Okay i did strip the PDT off the string and now: line = line.replace("PDT", "") rawtime = line dt = time.strptime(rawtime,'%a %b %d %H:%M:%S %Y' ) print dt shows time.struct_time(tm_year=2014, tm_mon=6, tm_mday=25, tm_hour=15, tm_min=38, tm_sec=29, tm_wday=2, tm_yday=176, tm_isdst=-1) now should i do a formatting one more time to put it in %Y=%m-%d %H:%M:%S??
So @dody to complete it you just save it in a variable x = str(dt.tm_year) + "-" + str(dt.tm_mon) + "-" + str(dt.tm_mday) + " " + str(dt.tm_hour) + ":" + str(dt.tm_min) + ":" + str(dt.tm_sec) hope that helps!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.