2010-07-23 529 views

回答

129

您可以使用datetime module在Python中處理日期和時間。 strftime method允許您使用您指定的格式生成日期和時間的字符串表示。

>>> import datetime 
>>> datetime.date.today().strftime("%B %d, %Y") 
'July 23, 2010' 
>>> datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y") 
'10:36AM on July 23, 2010' 
15
>>> import datetime 
>>> now = datetime.datetime.now() 
>>> now.strftime("%B %d, %Y") 
'July 23, 2010' 
3
#python3 

import datetime 
print(
    '1: test-{date:%Y-%m-%d %H:%M:%S}.txt'.format(date=datetime.datetime.now()) 
    ) 
d = datetime.datetime.now() 
print('2: {:%B %d, %Y}'.format(d)) 

1:測試2018-02-14_16:40:52.txt

2:2018年3月4日


描述:

使用新的stri ng格式將值注入到placeholder {}的字符串中,value是當前時間。

然後,不要僅僅將原始值顯示爲{},請使用格式化來獲取正確的日期格式。

https://docs.python.org/3/library/string.html#formatexamples

+0

您的答案被標記爲低質量,因爲它只是代碼。嘗試更深入地解釋你的答案。 – 2018-02-14 05:36:20

相關問題