只能在Python中将datetime对象转换为日期字符串 [英] Convert datetime object to a String of date only in Python

查看:134
本文介绍了只能在Python中将datetime对象转换为日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中将 datetime 字符串转换为 datetime 对象,我看到很多,但我想去另一种方式。

我有 datetime.datetime(2012,2,23,0,0),我想将其转换为字符串像'2/23/2012'

I see a lot on converting a datetime string to an datetime object in Python, but I want to go the other way.
I've got datetime.datetime(2012, 2, 23, 0, 0) and I would like to convert it to string like '2/23/2012'.

推荐答案

你可以使用 strftime 帮助您格式化日期。

You can use strftime to help you format your date.

例如,

t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')

将产生: / p>

will yield:

'02/23/2012'

有关格式设置的详细信息,请参阅此处

More information about formatting see here

这篇关于只能在Python中将datetime对象转换为日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆