Python日期转格式


修改日期显示格式  例如从  2022-12-16  变成 2022/12/16

 

 
 
import time


def DateFormat(t):
t = time.strptime(t, "/%Y/%m/%d") # 把字符串转换成 time.struct_time,%Y-%m-%d %H:%M:%S 为时间格式
t = time.mktime(t) # 把 time.struct_time 转换成对应的时间戳
t = time.localtime(t) # 把时间戳转换成 time.struct_time
t = time.strftime("%Y-%m-%d", t) # 把 'time.struct_time'转换成指定格式的字符串
print(t)

DateFormat('/2021/5/5')

%Y 对应 年
%m 对应 月
%d 对应 日
%H 对应 时
%M 对应 分
%S 对应 秒
 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM