亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python實(shí)現(xiàn)日期與天數(shù)的轉(zhuǎn)換

 更新時(shí)間:2025年06月29日 10:36:41   作者:憶杰  
本文主要介紹了python實(shí)現(xiàn)日期與天數(shù)的轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

閏年判斷

  • 一個(gè)年份是閏年當(dāng)且僅當(dāng)它滿足下列兩種情況其中的一種:
    • 這個(gè)年份是4 的整數(shù)倍,但不是100 的整數(shù)倍;
    • 這個(gè)年份是 400 的整數(shù)倍。

每個(gè)月的對(duì)應(yīng)的天數(shù)

  • 每年12 個(gè)月份,其中1,3,5,7,8,10,12,每個(gè)月有 31 天;
  • 4,6,9,11月每個(gè)月有 30 天;
  • 對(duì)于 2 月,閏年有 29 天,平年有 28 天。
import sys

# 日期處理-日期轉(zhuǎn)換為天數(shù),根據(jù)某年過去的天數(shù)獲取具體的日期
class DateHandle:
    def __init__(self) -> None:
        self.mp = {1:31, 3:31, 5:31, 7:31, 8:31, 10:31, 12:31, 4:30, 6:30, 9:30, 11:30, 2:28}  
    # 閏年判斷
    def check(self, year):
        if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
            return True
        return False
    # 獲取月份對(duì)應(yīng)的天數(shù)
    def init(self, year):
        if self.check(year): # 瑞年
            self.mp[2] = 29   
    # 根據(jù)年月日獲取對(duì)應(yīng)的天數(shù)
    def getDays(self, year, month, day):
        cnt = 0
        for m in range(1, month):
            cnt += self.mp[m]
        cnt += day
        return cnt
    # 根據(jù)某年的天數(shù)獲取年月日
    def getDate(self, year, days):
        cnt, month = 0, 1
        for i in range(1, 13):
            month = i
            if cnt + self.mp[i] <= days:
                cnt += self.mp[i]
            else:
                break    
        date = days - cnt 
        return "%d %d %d" % (year, month, date)  

year, month, day = 2025, 6, 6
d = DateHandle()
d.init(year)
cnt  = d.getDays(year, month, day)
date = d.getDate(year, cnt)
print('year: %d, month: %d, day: %d , days is %d' % (year, month, day, cnt))  
print('year is %d, days is %d, conver to date is %s' % (year, cnt, date))   

##------------------------output--------------------##
#	year: 2025, month: 6, day: 6 , days is 157
# 	year is 2025, days is 157, conver to date is 2025 6 6
##------------------------over----------------------##

到此這篇關(guān)于python實(shí)現(xiàn)日期與天數(shù)的轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)python 日期與天數(shù)轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論