Python:如何转换日期时间格式? [英] Python: How to convert datetime format?
本文介绍了Python:如何转换日期时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何将时间转换为字符串
我有 a
变量,如下面的代码所示。
I have a
variable as shown in the below code.
a = "2011-06-09"
使用python,如何将其转换为以下内容格式?
Using python, how to convert it to the following format?
"Jun 09,2011"
推荐答案
>>> import datetime
>>> d = datetime.datetime.strptime('2011-06-09', '%Y-%m-%d')
>>> d.strftime('%b %d,%Y')
'Jun 09,2011'
在2.5版之前的Python中,您可以将 datetime.strptime
替换为 time.strptime
,就像这样(未经测试) ): datetime.datetime(*(time.strptime('2011-06-09','%Y-%m-%d')[0:6]))
In pre-2.5 Python, you can replace datetime.strptime
with time.strptime
, like so (untested): datetime.datetime(*(time.strptime('2011-06-09', '%Y-%m-%d')[0:6]))
这篇关于Python:如何转换日期时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文