1. 程式人生 > >python3中輸入某年某月某日輸出某年第幾天(幾時幾刻)

python3中輸入某年某月某日輸出某年第幾天(幾時幾刻)

方法1:
year = int(input("請輸入年:"))
mouth = int(input("請輸入月:"))
day = int(input("請輸入日:"))
mouth_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
    mouth_day[1] = 29
print(sum(mouth_day[:mouth-1])+day)
方法2:
from datetime import datetime
new_day = datetime(2017
, 12, 31, 12, 36, 45) start_day = datetime(2017, 1, 1) print(new_day - start_day)
輸出的結果天數人為地再加1,還可以輸出幾時幾刻