1

So I'm doing some Ajax trickery in the front page, and in the DJango backend, I send a JS Object, using AJAX... the format is: 'Tue Jan 28 2014 00:00:00 GMT-0800 (PST)' So I'm trying to convert it to a Python object:

     import datetime
 81    if request.is_ajax():
 82       datestr = request.POST['from_date']
 83       date = datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S.%fZ").date()
 84       message = date.__str__()
 85    else:
 86       message = "Not Ajax"
 87 
 88    return HttpResponse(message)

However I'm getting the following error:

time data 'Tue Jan 28 2014 00:00:00 GMT-0800 (PST)' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

How could I fix that? I'm looking forward a nicer solution that would avoid splitting and parsing the string ...

7
  • do you have an option to change the date format sent? If yes, I would say change it to epoch.. Commented Jan 31, 2014 at 2:06
  • @Kiran how? I could easily parse the string, but I'm trying to avoid it Commented Jan 31, 2014 at 2:07
  • @mhlester not a duplicate ... problems are similar, but different Commented Jan 31, 2014 at 2:09
  • sorry you're right. retracted Commented Jan 31, 2014 at 2:10
  • I am not fully aware of how python works, but in javascript, you can get epoch by calling date.getTime()/1000. This answer http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python talks about converting epoch to needed format. I thought you have date object in your javascript based on what you are receiving. Commented Jan 31, 2014 at 2:14

1 Answer 1

2

Given the format in the error message, then the client you can use ES5s Date.prototype.toISOString() to convert a Date object to an ISO 8601 string. You'll need a polyfill for browsers that don't have it.

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.