日期时间格式超过24小时

5

我正在尝试添加时间并输出为hh:mm:ss,但是当时间超过24小时时,它会变成x天,hh:mm:ss。是否有办法仅在时间大于24小时时只显示hh:mm:ss

import datetime
from datetime import timedelta

# intro
print("Welcome to TimeCalc")

print("Calculating Durations")

clear = True
while clear == True:
    # first input
    firstNumber = True
    while firstNumber == True:
        time_1 = input("Please enter a time [hh:mm:ss] including 0s: ")
        if len(time_1) > 0 and len(time_1) >= 8 and time_1[-6] == ":" and time_1[-3] == ":" and int(time_1[-5:-3]) < 60 and int(time_1[-2:]) < 60:
            hours_1 = time_1[:-6]
            minutes_1 = time_1[-5:-3]
            seconds_1 = time_1[-2:]
            hours_1 = int(hours_1)
            minutes_1 = int(minutes_1)
            seconds_1 = int(seconds_1)
            firstNumber = False
        else:
            print("Invalid Syntax")
    time_1 = datetime.timedelta(hours=hours_1, minutes=minutes_1, seconds=seconds_1)

    cont = True
    while cont == True:
        # second input
        secondNumber = True
        while secondNumber == True:
            time_2 = input("Please enter a time to add [hh:mm:ss] including 0s: ")
            if len(time_2) > 0 and len(time_2) >= 8 and time_2[-6] == ":" and time_2[-3] == ":" and int(time_2[-5:-3]) < 60 and int(time_2[-2:]) < 60:
                hours_2 = time_2[:-6]
                minutes_2 = time_2[-5:-3]
                seconds_2 = time_2[-2:]
                hours_2 = int(hours_2)
                minutes_2 = int(minutes_2)
                seconds_2 = int(seconds_2)
                secondNumber = False
            else:
                print("Invalid Syntax")
        time_2 = datetime.timedelta(hours = hours_2, minutes = minutes_2, seconds = seconds_2)

        total = time_1 + time_2
        print("The total duration is: " + str(total))

        # continue, clear, or exit
        choice = input("Continue: Y | Clear: N | Exit: X: ")
        if choice == "Y" or choice == "y":
            time_1 = total
        elif choice == "N" or choice == "n":
            cont = False
        elif choice == "X" or choice == "x":
            quit()

既然我能够复现您的程序,那么您就可以得到一个赞。 - Ṃųỻịgǻňạcểơửṩ
2个回答

1
total变量之后,你可以尝试放置这段代码,也许这不是最好的解决方案,但它可以工作。
seconds = int(total.total_seconds())
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)

print("The total duration is: {h:02d}:{m:02d}:{s:02d}".format(h=hours,                                                                 
                                                        m=minutes, s=seconds))

1
@Mulliganaceous 如果他想要这个,他会去做的 :) 我只是提供了一个解决方案。 - Druta Ruslan
1
@Mulliganaceous 我加上这个 :) - Druta Ruslan
1
嗯,我现在正在寻找更好的解决方案。 - Druta Ruslan

0
使用.strftime()方法来格式化你的日期时间字符串。
例如,
>>> import datetime
>>> d = datetime.delta(hours=1)
>>> dt = datetime.datetime(2017,10,30,23,10,10) + d
>>> dt.strftime("%H:%M:%S")
'00:10:10'

希望它有所帮助。


“total”是一个没有“strftime”方法的时间差。 - Ṃųỻịgǻňạcểơửṩ
@Mulliganaceous 如果是这样,你可以将 datetime.datetime(0,0,0,0,0,0) 添加到那个 timedelta 对象中。我提供的只是一个提示。美丽胜于丑陋。 - lelouchkako

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接