本文實(shí)例講述了Python編程實(shí)現(xiàn)輸入某年某月某日計(jì)算出這一天是該年第幾天的方法。分享給大家供大家參考,具體如下:
#基于 Python3
一種做法:
def is_leap_year(year): # 判斷閏年,是則返回True,否則返回False if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: return True else: return Falsedef function1(year, month, day): # 計(jì)算給定日期是那一年的第幾天 leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if is_leap_year(year): result = sum(leap_year[:month - 1]) + day else: result = sum(no_leap_year[:month - 1]) + day return result
但是如果是你自己遇到了這樣的需求,那么就沒必要這么復(fù)雜了。因?yàn)镻ython內(nèi)置了完善的時(shí)間和日期處理函數(shù)。
import datetimeimport timedef function2(year, month, day): # 直接使用Python內(nèi)置模塊datetime的格式轉(zhuǎn)換功能得到結(jié)果 date = datetime.date(year, month, day) return date.strftime('%j')需要注意的是,上面的寫法里函數(shù)的參數(shù)分別是年月日的整數(shù),如果你想傳入字符串,比如"2016-10-1",那就需要先對(duì)字符串做處理了。
同樣的,也可以自己做或者用內(nèi)置函數(shù)。
# 假如輸入格式為字符串(比如從命令行讀入字符串2016-10-1),則需要先對(duì)輸入內(nèi)容進(jìn)行處理_input = '2016-10-1'_year1 = int(_input.split('-')[0])_month1 = int(_input.split('-')[1])_day1 = int(_input.split('-')[2])# 當(dāng)然你也可以用datetime的內(nèi)置方法進(jìn)行格式處理t = time.strptime(_input, '%Y-%m-%d')_year2 = t.tm_year_month2 = t.tm_mon_day2 = t.tm_mday下面是完整的代碼,測(cè)試"2016-10-1"的結(jié)果均為275。
import datetimeimport timedef is_leap_year(year): # 判斷閏年,是則返回True,否則返回False if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: return True else: return Falsedef function1(year, month, day): # 計(jì)算給定日期是那一年的第幾天 leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if is_leap_year(year): result = sum(leap_year[:month - 1]) + day else: result = sum(no_leap_year[:month - 1]) + day return resultdef function2(year, month, day): # 直接使用Python內(nèi)置模塊datetime的格式轉(zhuǎn)換功能得到結(jié)果 date = datetime.date(year, month, day) return date.strftime('%j')print(function1(2016, 10, 1))print(function2(2016, 10, 1))# 假如輸入格式為字符串(比如從命令行讀入字符串2016-10-1),則需要先對(duì)輸入內(nèi)容進(jìn)行處理_input = '2016-10-1'_split = _input.split('-')_year1 = int(_split[0])_month1 = int(_split[1])_day1 = int(_split[2])print(function1(_year1, _month1, _day1))print(function2(_year1, _month1, _day1))# 當(dāng)然你也可以用datetime的內(nèi)置方法進(jìn)行格式處理t = time.strptime(_input, '%Y-%m-%d')_year2 = t.tm_year_month2 = t.tm_mon_day2 = t.tm_mdayprint(function1(_year2, _month2, _day2))print(function2(_year2, _month2, _day2))# 后面發(fā)現(xiàn)我為了編函數(shù)寫復(fù)雜了,如果輸入是字符串其實(shí)一句話就好import time_input = '2016-10-1'# 詳見Python日期和字符串格式互相轉(zhuǎn)換 //m.survivalescaperooms.com/article/66019.htmt = time.strptime(_input, '%Y-%m-%d')print(time.strftime('%j',t))PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計(jì)算的在線工具供大家使用:
在線日期/天數(shù)計(jì)算器:
http://tools.VeVB.COm/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.VeVB.COm/bianmin/wannianli
在線陰歷/陽(yáng)歷轉(zhuǎn)換工具:
http://tools.VeVB.COm/bianmin/yinli2yangli
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時(shí)間操作技巧總結(jié)》、《Python URL操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選