最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年04月18日 14:32:28   作者:知乎  
這篇文章主要介紹了Python編程實現(xiàn)輸入某年某月某日計算出這一天是該年第幾天的方法,涉及Python針對日期時間的轉(zhuǎn)換與運算相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Python編程實現(xiàn)輸入某年某月某日計算出這一天是該年第幾天的方法。分享給大家供大家參考,具體如下:

#基于 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): # 計算給定日期是那一年的第幾天
  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ù)雜了。因為Python內(nèi)置了完善的時間和日期處理函數(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",那就需要先對字符串做處理了。

同樣的,也可以自己做或者用內(nèi)置函數(shù)。

# 假如輸入格式為字符串(比如從命令行讀入字符串2016-10-1),則需要先對輸入內(nèi)容進行處理
_input = '2016-10-1'
_year1 = int(_input.split('-')[0])
_month1 = int(_input.split('-')[1])
_day1 = int(_input.split('-')[2])
# 當(dāng)然你也可以用datetime的內(nèi)置方法進行格式處理
t = time.strptime(_input, '%Y-%m-%d')
_year2 = t.tm_year
_month2 = t.tm_mon
_day2 = t.tm_mday

下面是完整的代碼,測試"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): # 計算給定日期是那一年的第幾天
  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),則需要先對輸入內(nèi)容進行處理
_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)置方法進行格式處理
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ù)雜了,如果輸入是字符串其實一句話就好
import time
_input = '2016-10-1'
# 詳見Python日期和字符串格式互相轉(zhuǎn)換 http://m.fzitv.net/article/66019.htm
t = time.strptime(_input, '%Y-%m-%d')
print(time.strftime('%j',t))

PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計算的在線工具供大家使用:

在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli

在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結(jié)》、《Python URL操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論

阜南县| 辽中县| 汕头市| 栾城县| 克东县| 安福县| 浑源县| 东宁县| 章丘市| 张掖市| 天气| 青岛市| 宁化县| 大新县| 新巴尔虎右旗| 永德县| 上思县| 大新县| 武宣县| 扶沟县| 章丘市| 青神县| 科尔| 泗阳县| 庐江县| 繁峙县| 富蕴县| 鹤壁市| 东宁县| 老河口市| 武邑县| 宾阳县| 阿勒泰市| 青神县| 陇川县| 凉城县| 贵阳市| 顺义区| 张家川| 宣武区| 武安市|