Python strftime()无法格式化1900年份以前的问题解决方案
来源:互联网 发布:ubuntu 17.04 lts 编辑:程序博客网 时间:2024/06/05 05:11
datetime.strftime() 格式化1900年份以前时,会报出以下异常
import datetime
date_time = datetime.datetime(1989, 12, 31)
str_date_time = date_time.strftime('%Y-%m-%d')
此时会报出下面的错误
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
strftime()是不支持转化1900前的,此时可以通过str对象的format()方法根据需求格式化出想要的形式,format()方法原生支持datetime对象的格式化。 import datetime
date_time = datetime.datetime(1989, 12, 31)
str_date_time = '{0.year:4d}-{0.month:02d}-{0.day:02d}'.format(date_time)
str_date_time
output ‘1989-12-31’
0 0
- Python strftime()无法格式化1900年份以前的问题解决方案
- Python datetime 格式化字符串:strftime()
- python datetime模块用strftime 格式化时间
- python datetime模块用strftime 格式化时间
- python datetime模块用strftime 格式化时间
- hosts文件无法修改的问题解决方案。
- eclipse无法新建Class的问题解决方案
- 本地flash无法使用的问题解决方案
- PyCharm无法引用matplotlib的问题解决方案
- MySQL数据库无法连接的问题解决方案
- python发送邮件的乱码问题解决方案
- strftime 时间格式化
- 时间格式化函数strftime
- 时间格式化函数--strftime
- python date的strftime的时间格式
- Python乱码问题解决方案
- NetBeans的User Catelog定义无法保存的问题解决方案
- error LNK2019:无法解析的外部符号的问题解决方案
- 文件读入和输出
- 浅谈:nth-child和:nth-of-type
- SRM549 Div1 600
- 自动生成实体,Dao层代码
- Android问题集之编译冲突
- Python strftime()无法格式化1900年份以前的问题解决方案
- ARM Power On/Off Sequence and Power State
- linux 编译指定库、头文件的路径问题
- JAVA集合Set使用
- Android Studio2.3打包项目后,安装时提示无签名的解决方法
- 使用xhprof进行线上PHP性能追踪及分析
- 文件的上传
- Leetcode 239. Sliding Window Maximum
- 母函数