我制作了一個腳本來顯示今天的日期和您輸入的日期之間的差異,并以列印函式和 f 字串結束它。
from datetime import datetime
today = datetime.today()
print("Please enter the date you want to find out how many days until below: ")
year = int(input("What year? "))
month = int(input("What month? "))
day = int(input("What day? "))
date2 = datetime(year, month, day)
difference = date2 - today
print(f"There are only {difference.days 1} days left until {date2} from {today}")
它列印正確的資料,但它也顯示時間。所以它以這個為例:“從 2022-11-19 00:14:18.003365 到 2023-02-23 00:00:00 只剩下 96 天了”
我該如何洗掉時間?另外,如果有任何其他改進建議,我會洗耳恭聽。
uj5u.com熱心網友回復:
您可以date用于此計算。
from datetime import date
today = date.today()
print("Please enter the date you want to find out how many days until below: ")
year = int(input("What year? "))
month = int(input("What month? "))
day = int(input("What day? "))
date2 = date(year, month, day)
difference = date2 - today
print(f"There are only {difference.days 1} days left until {date2} from {today}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/537299.html
標籤:Python约会时间
