2016-10-02 50 views

回答

1

使用%I爲12小時倍和%pampm如下:

from datetime import datetime 

for t in ["3:14AM", "9:33PM", "12:21AM", "12:15PM"]: 
    print datetime.strptime(t, '%I:%M%p').strftime("%H:%M") 

給你以下的輸出:在strftime() and strptime() Behavior

03:14 
21:33 
00:21 
12:15 

使用的文檔格式的

0

你錯過%p(語言環境下的任上午或下午)。

time.strptime(string1,'%H:%M%p') 
1

的問題,您需要使用%I的12小時時間和增加對上午或下午指令%p得到一個時間對象第一strptime; altogther '%I:%M%p'。然後使用strftime將時間對象格式化成一個字符串:


試驗:

>>> tm = time.strptime('12:33AM', '%I:%M%p') 
>>> time.strftime('%H:%M', tm) 
'00:33' 
>>> tm = time.strptime('9:33PM', '%H:%M%p') 
>>> time.strftime('%H:%M', tm) 
'09:33' 

文件參考:https://docs.python.org/2/library/time.html

相關問題