I am trying to generate json date string for y’day. I am not sure how to generate it in RFC 3339 format.
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
def dthandler(obj):
return obj.isoformat()
with io.open('out/test.json', 'w', encoding='utf-8') as outfile:
json.dump(rdata, outfile, indent=4, sort_keys=True, default=dthandler)
which format the date as:
"Date": "2012-12-04",
How can I write the formatter function to get RfC 3339 format.
I use python 3.x
yesterdayis adatetime.dateobject. You could convert it to datetime.datetime object if you need time in the output:This should work both Python 2 and 3.