如何以JSON格式转换Python日期?
输入
from datetime import datetime
my_date = datetime.now()
像这样
{
"start_date": '2018-05-06T09:27:51.386383'
}
答案 0 :(得分:0)
使用strftime将datetime对象转换为字符串。
In [4]: from datetime import datetime
In [5]: import json
In [6]: start_date = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
In [7]: d = {'start_date': start_date}
In [8]: d
Out[8]: {'start_date': '2018-05-06T13:29:36.573828'}
In [9]: json.dumps(d)
Out[9]: '{"start_date": "2018-05-06T13:29:36.573828"}'
In [10]: