JSON encode ISO UTC datetime

1
2
3
4
5
6
7
import datetime
from dateutil import tz

def encode_datetime(obj):
	if isinstance(obj, datetime.datetime):
		return obj.astimezone(tz.tzutc()).strftime('%Y-%m-%dT%H:%M:%SZ')
	raise TypeError(repr(o) + " is not JSON serializable")

More like this

  1. isoutc template filter by japerk 3 years, 1 month ago
  2. JSON decode datetime by japerk 3 years, 1 month ago
  3. Encoding datetime for JSON consumers like YUI by acdha 2 years, 5 months ago
  4. timeto template filter by japerk 3 years, 1 month ago
  5. Another JsonResponse by kcarnold 3 years, 11 months ago

Comments

(Forgotten your password?)