For some reason, the function jsonify
converts mine datetime.date
to what seems like an HTTP date. How to save date in format yyyy-mm-dd
when using jsonify
?
test_date = datetime.date(2017, 4, 27)
print(test_date)
test_date_jsonify = jsonify(test_date)
print(test_date_jsonify.get_data(as_text=True))
As indicated in the comments, use jsonify(str(test_date))
returns the desired format. However, consider the following case:
test_dict = {"name": "name1", "date":datetime.date(2017, 4, 27)}
print(test_dict) # {"name": "name1", "date":datetime.date(2017, 4, 27)}
test_dict_jsonify = jsonify(test_dict)
print(test_dict_jsonify.get_data(as_text=True)) # {"date": "Thu, 27 Apr 2017 00:00:00 GMT", "name": "name1"}
test_dict_jsonify = jsonify(str(test_dict))
print(test_dict_jsonify.get_data(as_text=True)) # "{"date": datetime.date(2017, 4, 27), "name": "name1"}"
In this case, the solution str()
does not work.
source
share