About datetime module and JSON

import datetime
import simplejson as json

>> Encoder
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
json.dumps(datetime.datetime.now(), default=dthandler)

>> Decoder
try:
v = datetime.datetime.strptime(v, '%Y-%m-%dT%H:%M:%S.%f)
except ValueError:
try:
v = datetime.datetime.strptime(v, '%Y-%m-%d').date()
except ValueError:
..........

Comments

Popular posts from this blog

Changing postgres password and warning