---
title: "JSON datetime between Python and JavaScript"
author: "eonbell"
date: "2011-09-05T09:56:28Z"
tags: []
categories: ["Web前端"]
slug: "json-datetime-between-python-and-javascript"
draft: false
lastmod: "2011-09-05T09:56:28Z"
id: 1165566
url: "https://eonbell.iteye.com/blog/1165566"
---

import json    
json.dumps(d.isoformat())
var d = new Date("2011-05-25T13:34:05.787000");
As of Javascript 1.8.5, Date objects have a toJSON method, which returns a string in a standard format. To serialize the above Javascript object back to JSON, therefore, the command would be:

d.toJSON()

Which would give you:

'2011-05-25T20:34:05.787Z'

This string, once received in Python, could be deserialized back to a datetime object:

datetime.strptime('2011-05-25T20:34:05.787Z', '%Y-%m-%dT%H:%M:%S.%fZ')

This results in the following datetime object, which is the same one you started with and therefore correct:

datetime.datetime(2011, 5, 25, 20, 34, 5, 787000)