Convert datetime object to a String of date only i

2019-01-02 16:58发布

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'.

8条回答
千与千寻千般痛.
2楼-- · 2019-01-02 17:46

You can use strftime to help you format your date.

E.g.,

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

will yield:

'02/23/2012'

More information about formatting see here

查看更多
梦该遗忘
3楼-- · 2019-01-02 17:51

You can convert datetime to string. published_at="{}".format(self.published_at)

查看更多
登录 后发表回答