Home > Backend Development > Python Tutorial > Let python json encode datetime type

Let python json encode datetime type

高洛峰
Release: 2017-01-14 15:55:42
Original
1849 people have browsed it

The implementation code is as follows:

import json 
from datetime import date, datetime 


def __default(obj): 
if isinstance(obj, datetime): 
return obj.strftime('%Y-%m-%dT%H:%M:%S') 
elif isinstance(obj, date): 
return obj.strftime('%Y-%m-%d') 
else: 
raise TypeError('%r is not JSON serializable' % obj) 

print json.dumps({'d': datetime.now(), 'today': date.today(), 'x': 111}, 
default=__default)
Copy after login

For more articles related to python json encode datetime type, please pay attention to the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Python converts json to lua table
From 1970-01-01 08:00:00
0
0
0
python3.x - python processing javascript json
From 1970-01-01 08:00:00
0
0
0
Convert Html to Json using Python
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template