Python:如何转换日期时间格式?

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]))