优达学城计算机科学导论:python 两日期间的天数

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    def daysinyear(year,month,day):#the day of this year
        count=0
        if year%400==0 or (year%100!=0 and year%4==0):
            daysOfMonths1 = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
            for i in range(month-1):
                count += daysOfMonths1[i]
            return count + day
        else:
            daysOfMonths2 = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
            for i in range(month-1):
                count += daysOfMonths2[i]
            return count + day  
    import calendar
    def daysofyear(year1,year2):# the day of (year2-year1)
        minusyearsdays=(year2-year1)*365+calendar.leapdays(year1,year2)
        return minusyearsdays
    total = daysinyear(year2, month2, day2)-daysinyear(year1, month1, day1)+daysofyear(year1,year2)
    return total
        

计算两不同日期间的天数 难点在于每月日期不同 存在平年闰年 借鉴了其他大神的日期函数 将年份和月份拆开分别计算天数再进行求和运算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值