新聞中心
python判斷是哪一天的

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產品對外擴展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,創(chuàng)新互聯(lián)公司面向各種領域:成都砂巖浮雕等網(wǎng)站設計、成都全網(wǎng)營銷推廣解決方案、網(wǎng)站設計等建站排名服務。
方法1:先判斷是否是閏年,然后再利用求和,得出某一天是第幾天
# 方法1:low版 def func1(year, month, day): # 分別創(chuàng)建平年,閏年的月份天數(shù)列表(注意列表下標從0開始,而月份是從1月開始,所以1月份對應的下標是0) 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 (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: # 對列表進行切片處理,獲取該月份及之前的所有月份,并對月份天數(shù)求和,最后再加上該月份的某一日,即可獲得該日期是一年中的第幾天 result = sum(leap_year[:month - 1]) + day return result else: result = sum(no_leap_year[:month - 1]) + day return result print(func1(2019, 6, 20)) # 171
方法2:使用datetime模塊
# 方法2 datetime模塊 import datetime def func2(y, m, d): sday = datetime.date(y, m, d) # print(type(sday), sday) #2019-06-20 # print(sday.month) # 6 count = sday - datetime.date(sday.year - 1, 12, 31) # 減去上一年最后一天 # 2017-12-31 print(count, type(count)) # 171 days, 0:00:00 return '%s是%s年的第%s天' % (sday, y, count.days) print(func2(2019, 6, 20)) # 171
方法3:使用內置函數(shù)strftime
strftime是一種計算機函數(shù),根據(jù)區(qū)域設置格式化本地時間/日期,函數(shù)的功能將時間格式化,或者說格式化一個時間字符串。
# 方法3
import datetime
def func3(year, month, day):
date = datetime.date(year, month, day)
return date.strftime('%j') # %j十進制表示的每年的第幾天
print(func3(2019, 6, 20)) # 171
網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:Python怎么判斷是哪一天
轉載來于:http://www.5511xx.com/article/cdjhhgc.html


咨詢
建站咨詢
