Python編程實(shí)現(xiàn)輸入某年某月某日計(jì)算出這一天是該年第幾天的方法
本文實(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 False
def 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 datetime
import time
def 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 datetime
import time
def is_leap_year(year): # 判斷閏年,是則返回True,否則返回False
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
def 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
def 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_mday
print(function1(_year2, _month2, _day2))
print(function2(_year2, _month2, _day2))
# 后面發(fā)現(xiàn)我為了編函數(shù)寫復(fù)雜了,如果輸入是字符串其實(shí)一句話就好
import time
_input = '2016-10-1'
# 詳見Python日期和字符串格式互相轉(zhuǎn)換 http://www.dhdzp.com/article/66019.htm
t = time.strptime(_input, '%Y-%m-%d')
print(time.strftime('%j',t))
PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計(jì)算的在線工具供大家使用:
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬(wàn)年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽(yáng)歷轉(zhuǎn)換工具:
http://tools.jb51.net/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ì)有所幫助。
- python輸出指定月份日歷的方法
- python實(shí)現(xiàn)根據(jù)月份和日期得到星座的方法
- python 計(jì)算兩個(gè)日期相差多少個(gè)月實(shí)例代碼
- 利用python獲取當(dāng)前日期前后N天或N月日期的方法示例
- Python實(shí)現(xiàn)按當(dāng)前日期(年、月、日)創(chuàng)建多級(jí)目錄的方法
- python獲取指定日期范圍內(nèi)的每一天,每個(gè)月,每季度的方法
- python 輸出上個(gè)月的月末日期實(shí)例
- Python 日期區(qū)間處理 (本周本月上周上月...)
- Python實(shí)例教程之檢索輸出月份日歷表
相關(guān)文章
Pytorch?使用Google?Colab訓(xùn)練神經(jīng)網(wǎng)絡(luò)深度學(xué)習(xí)
本文以VOC數(shù)據(jù)集為例,因此在訓(xùn)練的時(shí)候沒有修改classes_path等,如果是訓(xùn)練自己的數(shù)據(jù)集,各位一定要注意修改classes_path等其它參數(shù)2022-04-04
Python調(diào)用GPT3.5接口的最新方法實(shí)例詳解
這篇文章主要介紹了Python調(diào)用GPT3.5接口的最新方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Python數(shù)據(jù)類型之Set集合實(shí)例詳解
這篇文章主要介紹了Python數(shù)據(jù)類型之Set集合,結(jié)合實(shí)例形式詳細(xì)分析了Python數(shù)據(jù)類型中集合的概念、原理、創(chuàng)建、遍歷、交集、并集等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
Python2.7版os.path.isdir中文路徑返回false的解決方法
這篇文章主要為大家詳細(xì)介紹了Python2.7版os.path.isdir中文路徑返回false的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Python爬蟲使用瀏覽器cookies:browsercookie過程解析
這篇文章主要介紹了Python爬蟲使用瀏覽器cookies:browsercookie,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

