不需要的输出

时间:2019-06-06 15:58:34

标签: python

我只使用日期而不是日期时间,但是在输出中我收到此“给定日期之间的天数是4天,0:00:00”,为什么这里是零。

from datetime import date
while True:
    while True:
                try:
                    print("\nInsert appropriate values for first date.")
                    year1 = int(input("\nEnter a year:"))
                    month1 = int(input("Enter a month:"))
                    day1 = int(input("Enter a day:"))
                except ValueError:
                    print("\nPlease enter only number")
                else:
                    break
    while True:
                try:
                    print("\nInsert appropriate values for second date.")
                    year2 = int(input("\nEnter  year:"))
                    month2 = int(input("Enter  month:"))
                    day2 = int(input("Enter  day:"))
                except ValueError:
                    print("\nPlease enter only number:")
                else:
                    break
    date1 =date(year1,month1,day1)
    date2 =date(year2,month2,day2)
    num_of_days=(date2 - date1)
    print("\nThe days between the given dates is/are",num_of_days)
    while True:
        Repeat=input("\nDo you want to calculate again\n\nYes or No:")
        Repeat=Repeat.lower()
        if Repeat not in ["yes","y","no","n"]:
            print("\nPlease select correct option")
        else:
            break


    if Repeat in ["yes","y"]:
        continue
    else:
        if Repeat in ["no","n"]:
            print("\n-----Thank you for using-----")
            input()
            break

送礼者日期之间的天数是13天,0:00:00 这里(13天,0:00:00)这里是13天,即数字0:00:00为什么会出现在输出中以及什么是解决方案。

2 个答案:

答案 0 :(得分:1)

您可以打印num_of_days.days,只需替换以下行:

print("\nThe days between the given dates is/are", num_of_days.days)

如果日期是相反的,也许您也希望拥有abs,而您不希望天数为负。

print("\nThe days between the given dates is/are", abs(num_of_days.days))

答案 1 :(得分:0)

数字0:00:00datetime的时间部分。如果您只想要没有时间的日期,则只需使用date