Python:Flask使用jsonify格式化时间
Python 使用 时间 格式化 Flask jsonify
2023-06-13 09:11:41 时间
大家好,又见面了,我是你们的朋友全栈君。
代码如下
# -*- coding: utf-8 -*-
from datetime import datetime, date
from flask.json import JSONEncoder
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
else:
return JSONEncoder.default(self, obj)
使用
from datetime import datetime
from flask import Flask, jsonify
app = Flask(__name__)
# 替换默认的json编码器
app.json_encoder = CustomJSONEncoder
@app.route('/test')
def test_world():
time = datetime.now()
return jsonify({
'time': time})
效果
{
"time": "2020-09-17 16:31:55"
}
参考 关于python:使用Flask的jsonify时,将datetime.date保持为’yyyy-mm-dd’格式
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/144142.html原文链接:https://javaforall.cn
相关文章
- pycharm使用远程python虚拟环境_pycharm自带python吗
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- python hashlib_Python hashlib模块实例使用详解
- python监控网页内容变化_使用Python监控文件内容变化代码实例
- 某大厂面试题:如何只用python的内置函数处理10G的大文件并使使用内存最小
- 【说站】python析构函数的使用注意
- 使用PYTHON中KERAS的LSTM递归神经网络进行时间序列预测|附代码数据
- lambda python表达式_Python的条件表达式和lambda表达式实例
- 力扣刷题笔记--13. 罗马数字转整数 python解法
- python使用opencv如何保存图片_OpenCV Python 保存图片
- python--配置文件以及ConfigParser使用
- 云原生之使用Docker部署Python应用
- 软件测试|Python内置模块使用(一)
- 使用 cProfile 和火焰图调优 Python 程序性能
- python中 itertools模块的使用方法
- python-Python与SQLite数据库-使用Python执行SQLite查询(二)
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(一)
- python-Python与PostgreSQL数据库-使用Python执行PostgreSQL查询(一)
- Python中使用MongoEngine详解编程语言
- Python是什么
- 使用Python连接SQL Server数据库(python连接sqlserver数据库)
- Python玩转Redis:提升缓存效率(python使用redis)
- 使用 requests 访问 Python 包索引(PyPI)的 JSON API
- python驱动使用pip安装MySQL Python驱动的简单步骤(pip安装mysql)
- Linux下运行Python脚本的步骤简介(linux如何运行python脚本)
- 在Linux系统中使用Python编程语言(linux中python)
- 使用python解析xml成对应的html示例分享
- python中将字典转换成其json字符串