2022-04-26写成‘2022年04月26日’python怎么写
你可以使用Python中的datetime模块来实现日期的转换。下面是一个示例代码:
from datetime import datetime
date_str = '2022-04-26'
date_obj = datetime.strptime(date_str, '%Y-%m-%d')
formatted_date = date_obj.strftime('%Y年%m月%d日')
print(formatted_date)
输出结果为:
2022年04月26日
在代码中,首先使用strptime方法将字符串类型的日期转换为datetime对象,然后使用strftime方法将datetime对象转换为指定格式的字符串。在strftime方法中,%Y代表四位数的年份,%m代表两位数的月份,%d代表两位数的日期。
原文地址: https://www.cveoy.top/t/topic/ihhF 著作权归作者所有。请勿转载和采集!