2013-10-02 100 views
0

應該很簡單。我有Python中的DateTime對象,我需要它的形式python中的日期時間格式化

20131002 

有沒有一種方法,而不必訴諸打破的日期爲它的組成部分,並手動將它們整合在一起的字符串格式化這個的?

+1

你有看'datetime.strftime'? –

+0

謝謝。那很簡單。 你會推薦刪除這個問題嗎? DID的問題得到了解答,但現在看起來似乎微不足道。 – Zack

+0

我已經發布了這個答案。 –

回答

1

這是在Python datetime庫超級簡單:

from datetime import datetime 

test = datetime.now()  # This is just some test datetime object 
test.strftime('%Y%m%d') # This is the format statement 

然而,快速谷歌搜索顯示了我,你會通過搜索「蟒蛇日期時間格式」中找到相似的答案。

1

使用datetime.strftime

>>> from datetime import datetime 
>>> dt = datetime.now() 
>>> dt.strftime('%Y%m%d') 
'20131003'