[英]Convert datetime into standard time python
我正在嘗試將以小時分鍾和秒顯示的時間數據轉換為標准時間。 我也想丟棄秒數。 我正在像這樣從 mysql 中選擇日期時間
time = connect("SELECT","""SELECT datTime FROM myTable WHERE ID = 1;""")
然后我這樣做只有時間
time = time[0][0].time()
而且我嘗試了各種將時間轉換為標准時間減去秒數的方法,例如
noSeconds = time.strftime("%H:%M")
timeNoSec = datetime.strptime(noSeconds, '%H:%M')
standardTime = noSeconds.strftime('%I:%M %p')
print(standardTime)
但我只是收到以下錯誤
standardTime = noSeconds.strftime('%I:%M %p')
AttributeError: 'str' object has no attribute 'strftime'
這是因為變量 time 是一個字符串而不是時間格式,所以你嘗試的方法都不起作用。 要轉換我發現了這個:
time_total = time.strptime(time, "%Y%m%d%H%M%S") #here you have to put all the received time format
timeNoSec = time_total.strftime("%H:%M")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.