2011-10-01 42 views
1

如果我有一個DateTime對象,我怎麼會得到的日期,如以下格式的字符串:刪除前導0對STR(日期)

1/27/1982 # it cannot be 01/27/1982 as there can't be leading 0's 

我做它目前的方法是做一個。替換所有的數字(01,02,03等),但這看起來非常低效且麻煩。什麼是更好的方法來完成這個?

謝謝。

+0

這似乎是http://stackoverflow.com/q/2309828/113527 – Martey

回答

4

你可以自己設置其格式,而不是使用strftime

'{0}/{1}/{2}'.format(d.month, d.day, d.year) // Python 2.6+ 

'%d/%d/%d' % (d.month, d.day, d.year) 
+2

重複我認爲這將是爲更好:''{0.month}/{0.day}/{ '年'。格式(d)' –

+1

OP似乎要求先在月份。 –

+0

@Jeff:同意,這很不錯。 – bobince