How to intercept datetime value and make custom format?
Here is how i fetch records and put them in json format:
cursor.execute(sql)
row_headers = [x[0] for x in cursor.description]
json_data = []
for row in cursor.fetchall():
json_data.append(dict(zip(row_headers, row)))
return jsonify(json_data)
When I send to console i have something like:
('2022000390100002', '00002', 'CTD2X405AXXX', '2022/39/1', 48.0, datetime.date(2022, 12, 20), 4, None)
The json output looks like:
{
"BoxNumber": "00002",
"BoxQty": 48.0,
"DateofSPC": "Tue, 20 Dec 2022 00:00:00 GMT",
"GPDI_Codice": null,
"Lot": "2022000390100002",
"OrderNumber": "2022/39/1",
"Product": "CTD2X405AXXX",
"TestResult": 4
}
What I want to do is to change DateofSPC output to dd.mm.yyyyy
You may test the type of the column:
from datetime import datetime
for row in cursor.fetchall():
if type(row) is datetime:
row = datetime.strptime(row, "%a, %d %b %Y %H:%M:%S %Z")
row = row.strftime('%d.%m.%Y')
json_data.append(dict(zip(row_headers, row)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.