json_dump_date.py 513 B

123456789101112131415161718192021
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: Deepcold
  5. @file: json_dump_date.py
  6. @time: 2019/8/21 8:51
  7. """
  8. from datetime import date
  9. import json
  10. from datetime import datetime
  11. class JsonCustomEncoder(json.JSONEncoder):
  12. def default(self, value):
  13. if isinstance(value, datetime):
  14. return value.strftime('%Y-%m-%d %H:%M:%S')
  15. elif isinstance(value, date):
  16. return value.strftime('%Y-%m-%d')
  17. else:
  18. return json.JSONEncoder.default(self, value)