0

I have the following string

date = "Thu May 08 2014 12:06:43 GMT+0300 (EEST)"

How can I turn it in to a valid python datetime object using stptime?

I did this

datePy = datetime.strptime(date, "%a, %d %b %Y %H:%M:%S (%Z)")

but didn't work. The traceback

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'Thu May 08 2014 12:06:43 GMT+0300 (EEST)' does not match format '%a, %d %b %Y %H:%M:%S (%Z)'

For some background details I get the date string from javascript Date.toString() function send it to my django back end and want to turn it to python datetime object that is naive.

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • 1
    What do you mean it didn't work? What happened? – Timmy O'Mahony May 08 '14 at 09:26
  • Edited my post and added the traceback – Apostolos May 08 '14 at 09:32
  • what is the UTC time for the provided date string -- is it `15:06` or `09:06`? Note: it seems `dateutil.parser.parse()` returns wrong `15:06` if EEST stands for Eastern European Summer Time. – jfs May 08 '14 at 10:28
  • I decided to take a different path. I only need local time, so I took the Date.toUTCString() which returns a string that can be easily transformed to a datetime object. I also used the Date.getTimeZoneOffset() to get the time difference in minutes. Then in my view I used timedelta to transform the initial datetime object (UTC) to local time. – Apostolos May 08 '14 at 11:49
  • @ J.F. Sebastian UTC for the date string was 09:06 and getTimeZoneOffset() returns -180 – Apostolos May 08 '14 at 11:50

1 Answers1

0

You aren't using the correct formatting (which is quite clear from the error). For example, you have a , after the %a which isn't there in your string and %d (Day of the month as a zero-padded decimal number.) instead of %B (Month as locale’s full name). Try:

datePy = datetime.strptime(date, "%a %B %d %Y %H:%M:%S (%Z)")
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • You need to figure it out yourself, the error is quite clear and I've shown you the reference you need. Read the reference I gave you and match it up to the correct format string. For example, it's not clear from your example whether or not your month string is abbreviated. If so (i.e. "Dec" instead of "Decemeber") you need to use `%b` instead of `%B`. – Timmy O'Mahony May 08 '14 at 09:39
  • Thank you. The reference you sent me has a %z that returns a value error. Thanks for trying, hope I can figure it out..The problem is the +0300 and (EEST) which are nowhere described in strptime how to convert them. Thanks anyway. – Apostolos May 08 '14 at 09:45
  • Ok, the problem is with the timezones. Have a look at [this question](http://stackoverflow.com/questions/3305413/python-strptime-and-timezones). The answer for the bounty recommends this library: [`python-dateutil`](http://labix.org/python-dateutil) – Timmy O'Mahony May 08 '14 at 09:52
  • Thx. But I would like to see first if it can be solved without installing any other extra libraries. – Apostolos May 08 '14 at 11:51