我想將時間分開,以便在串列中列出小時、分鐘和秒。但是使用 datetime 模塊我不能。這是我的代碼:
heure = datetime.now().time()
heure = heure.split(":")
print(heure)
先感謝您。
uj5u.com熱心網友回復:
heure是一個datetime.time物件,而不是一個字串。它已經將時間的各個組成部分作為屬性進行了拆分。做:
>>> hms = [heure.hour, heure.minute, heure.second]
>>> hms
[8, 9, 33]
uj5u.com熱心網友回復:
您需要heure使用 the 將其轉換為字串,str()然后將其拆分。
heure = datetime.now().time()
heure = str(heure).split(":")
print(heure)
uj5u.com熱心網友回復:
有一個名為 的方法datetime.strftime,它也datetime像您嘗試使用的方法一樣使用。我們使用 將字串輸入更改為整數int()。
from datetime import datetime as dt # Import datetime module
now = dt.now()
# Using strftime to get the string into a global variable
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
hour = now.strftime("%H")
minute = now.strftime("%M")
second = now.strftime("%S")
microseconds = now.strftime("%f")
# Turning the variables into integers
year = int(year)
month = int(month)
day = int(day)
hour = int(hour)
minute = int(minute)
second = int(second)
microseconds = int(microseconds)
# Putting the variables into a list
heure = [year, month, day, hour, minute, second, microseconds]
print(heure)
heure 串列的索引
索引 0:年份
指數一:月
索引 2:天
索引 3:小時
索引 4:分鐘
索引 5:秒
索引 6:微秒(1/1 000 000 秒)
The only problem is that the time is in UTC, sadly, I do NOT know how to convert it using
datetime.now().
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435936.html
