Jos*_*osh 22 python time datetime strftime strptime
我正在尝试将字符串"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)')
Run Code Online (Sandbox Code Playgroud)
给
AttributeError: 'time.struct_time' object has no attribute 'strftime'
显然,我犯了一个错误:时间错了,它是一个日期时间对象!它有一个日期和时间组件!
>>>import datetime
>>>s = datetime.strptime("20091229050936", "%Y%m%d%H%M%S")
Run Code Online (Sandbox Code Playgroud)
给
AttributeError: 'module' object has no attribute 'strptime'
我是怎么意思将字符串转换为格式化的日期字符串?
sth*_*sth 41
为datetime对象,strptime是一个静态方法的的datetime类,而不是在无功能datetime模块:
>>> import datetime
>>> s = datetime.datetime.strptime("20091229050936", "%Y%m%d%H%M%S")
>>> print s.strftime('%H:%M %d %B %Y (UTC)')
05:09 29 December 2009 (UTC)
Run Code Online (Sandbox Code Playgroud)
Jos*_*osh 12
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)
Run Code Online (Sandbox Code Playgroud)
给
05:09 29 December 2009 (UTC)
| 归档时间: |
|
| 查看次数: |
74106 次 |
| 最近记录: |