1. 程式人生 > >Python 判斷閏年,判斷日期是當前年的第幾天

Python 判斷閏年,判斷日期是當前年的第幾天

也有 www 方法 uno 判斷閏年 style http sum pre

http://www.cnblogs.com/vamei/archive/2012/07/19/2600135.html

Python小題目 針對快速教程 作業答案

寫一個程序,判斷2008年是否是閏年。

寫一個程序,用於計算2008年10月1日是這一年的第幾天?(2008年1月1日是這一年的第一天)

 1 #判斷閏年
 2 def is_leap_year(year):
 3     return  (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
 4 #判斷是這一年的第幾天
 5 def getDayInYear(year,month,day):
 6     month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
7 if is_leap_year(year): 8 month_day[1]=29 9 return sum(month_day[:month - 1]) + day 10 11 print(getDayInYear(2008,1,1)) 12 print(getDayInYear(2008,10,1)) 13 print(getDayInYear(2009,10,1))

也有現成的方法 time strftime() 參考該方法說明 點擊鏈接 http://www.runoob.com/python/att-time-strftime.html

import
datetime def getDayInYear(year, month, day): date = datetime.date(year, month, day) return date.strftime(%j) print(getDayInYear(2008,1,1)) print(getDayInYear(2008,10,1)) print(getDayInYear(2009,10,1))

Python 判斷閏年,判斷日期是當前年的第幾天