简体   繁体   中英

Python print time as days and hours

Need to format time into a readable format like: input:

print(uptime) 21 days, 7:35:25.686738

desired output: 21 days, 7 hours

You could try something like this:

date_str = str(uptime)
days, times = date_str.split(", ")
result = days
times = times.split(":")
units = ["hours", "minutes", "seconds"]
for i, unit in enumerate(units):
    result += f", {times[i]} {unit}"
print(result)

Basically, I'm constructing the string manually here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM