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?
![]() ![]() ![]() 12
|
|||
|
![]() ![]() |
For cross language projects I found out that strings containing RfC 3339 dates are the best way to go. A RfC 3339 date looks like this:
I think most of the format is obvious. The only somewhat unusual thing may be the "Z" at the end. It stands for GMT/UTC. You could also add a timezone offset like +02:00 for CEST (Germany in summer). I personally prefer to keep everything in UTC until it is displayed. For displaying, comparisons and storage you can leave it in string format across all languages. If you need the date for calculations easy to convert it back to a native date object in most language. So generate the JSON like this:
Unfortunately Javascripts Date constructor doesn't accept RfC 3339 strings but there are many parsers available on the Internet. |
||||||
|
![]() ![]() |
If you're certain that only Javascript will be consuming the JSON, I prefer to pass Javascript The
Javascript will happily use that as an object literal, and you've got your Date object built right in. |
||||
|
![]() ![]() |
I can address you to eGenix Python extension, containing a lot of functions for handling date and time. Plus, i've found this article with some code to deal with Python to Javascript marshaling. |
||
|