当前位置: 移动技术网 > IT编程>脚本编程>Python > 使用Python将字符串转换为格式化的日期时间字符串

使用Python将字符串转换为格式化的日期时间字符串

2019年09月01日  | 移动技术网IT编程  | 我要评论

杏坛门,火影忍者纲手被轮图,特朗普攻击媒体

我正在尝试将字符串“20091229050936”转换为“2009年12月29日(utc)”

>>>import time
>>>s = time.strptime("20091229050936", "%y%m%d%h%m%s")
>>>print s.strftime('%h:%m %d %b %y (utc)')

attributeerror: 'time.struct_time' object has no attribute 'strftime'

显然,我犯了一个错误:时间错了,它是一个日期时间对象!它有一个日期时间组件!

>>>import datetime
>>>s = datetime.strptime("20091229050936", "%y%m%d%h%m%s")

attributeerror: 'module' object has no attribute 'strptime'

我是怎么意思将字符串转换为格式化的日期字符串?

 

解决方案


time.strptime返回time_struct; time.strftime接受a time_struct作为可选参数:

>>>s = time.strptime(page.edittime(), "%y%m%d%h%m%s")
>>>print time.strftime('%h:%m %d %b %y (utc)', s)

05:09 29 december 2009 (utc)


本文首发于,博客园同步跟新

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网