隨機日期求離本年的天數

2021-10-16 01:11:54 字數 955 閱讀 7903

隨機日期求離本年的天數

day =

input

('請輸入年月日(格式舉例:2000-01-05):'

)year =

int(day[:4

])# 將年份擷取

month =

int(day[5:

7])# 擷取月份

sun =

int(day[8:

10])# 擷取日

print

(year, month, sun)

t_run =[31

,29,31

,30,31

,30,31

,31,30

,31,30

,31]# 閏年每個月天數

f_run =[31

,28,31

,30,31

,30,31

,31,30

,31,30

,31]# 平年每個月的天數

tall_day =

0# 設總天數初始值為0

if year %4==

0and year %

100!=

0or year %

400==0:

# 判斷閏年的條件

for i in

range

(month -1)

:# 本月之前的所有月份天數迴圈求和

tall_day += t_run[i]

else

:for i in

range

(month -1)

: tall_day += f_run[i]

print

('您輸出的是{}年的第{}天'

.format

(year, tall_day + sun)

)

輸入兩個日期求相隔的天數

import datetime defis runyear years 判斷乙個年份是否是閏年 return years 4 0and years 100 0or years 400 0def for str data data 格式化字串,將字串轉化為列表 2018 1 1 2018,1,1 re...

知道兩個日期,求相隔天數c程式

已知起始 終止日期,求相隔天數 2019,2,6 include int year,month,day int startyear,startmonth,startday 宣告起始日期 int endyear,endmonth,endday 宣告終止日期 int monthayear void he...

輸入日期求星期的程式

week.c version 1.0 2006 9 14 auther jsp w d 2 m 3 m 1 5 y y 4 y 100 y 400 mod 7 在公式中d表示日期中的日數,m表示月份數,y表示年數。注意 在公式中有個與其他公式不同的地方 把一月和二月看成是上一年的十三月和十四月,例 ...