Python datetime.strftime() 方法:日期时间格式化指南
strftime() 方法是 Python 中 datetime 模块中的一个函数,用于将日期时间对象转换成指定格式的字符串。
语法:
datetime对象.strftime(格式字符串)
参数说明:
datetime对象:需要转换的日期时间对象。格式字符串:指定转换后的格式,由特定的格式化代码组成。
常用的格式化代码如下:
%Y:4 位数的年份表示 (0000-9999)%m:月份 (01-12)%d:月内中的一天 (01-31)%H:24 小时制小时数 (00-23)%M:分钟数 (00-59)%S:秒数 (00-59)
示例:
from datetime import datetime
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_date)
输出结果:
2022-01-01 12:30:45
上述示例将当前时间格式化为'年-月-日 时:分:秒'的形式。
原文地址: https://www.cveoy.top/t/topic/qC1J 著作权归作者所有。请勿转载和采集!