国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python編程實(shí)現(xiàn)輸入某年某月某日計(jì)算出這一天是該年第幾天的方法

2019-11-25 16:14:33
字體:
供稿:網(wǎng)友

本文實(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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 始兴县| 福州市| 五河县| 睢宁县| 额敏县| 工布江达县| 永寿县| 嘉善县| 界首市| 松原市| 夹江县| 区。| 蓬溪县| 仪征市| 涿州市| 林州市| 湾仔区| 永靖县| 泾阳县| 华阴市| 青岛市| 大名县| 长白| 玛曲县| 潍坊市| 社会| 夏津县| 延长县| 富锦市| 莎车县| 都安| 龙岩市| 论坛| 如皋市| 安平县| 江北区| 建水县| 奈曼旗| 措勤县| 宣恩县| 富蕴县|