python 计算两个日期间隔年月日


由于项目需要计算租赁合同,起租日与终止日 间隔期限,找了半天是以365天算一年30天算一个月,不是自己想要的结果

一跨平闰年就有问题!不知道还有没有好的方法,下面是两种算间隔的方法


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   calc_date_diff.py
@Contact :   [email protected]
@License :   (C)Copyright 2018-2019

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2019/8/23 15:23   zht_hai      1.0         None
'''
import calendar
import datetime

start_date = datetime.date(2019, 7, 3)
end_date = datetime.date(2021, 6, 3)

print("日期间隔天数===>",(end_date-start_date).days)
print(start_date.year,start_date.month,start_date.day)
print(end_date.year,end_date.month,end_date.day)

def getMonthFirstDayAndLastDay(year=int, month=int):
    """
    计算日期当前月份起始第一天与最后一天
    :param year: 年份,int类型
    :param month: 月份,int类型
    :return: firstDay: 当月的第一天
              lastDay: 当月的最后一天
    """
    firstDayWeekDay, monthRange = calendar.monthrange(year, month)
    firstDay = datetime.date(year=year, month=month, day=1).day
    lastDay = datetime.date(year=year, month=month, day=monthRange).day
    return firstDay, lastDay

def calcDateDiff(start_date=datetime, end_date=datetime):
    """
    :param start_date:
    :param end_date:
    :return:  日期间隔年月天
    """
    middle_str = ''
    start_date_first_day,start_date_last_day = getMonthFirstDayAndLastDay(start_date.year,start_date.month)
    end_date_first_day, end_date_last_day = getMonthFirstDayAndLastDay(end_date.year, end_date.month)
    # 同年
    if start_date.year == end_date.year:
        if (start_date.day == start_date_first_day and end_date.day == end_date_last_day) and start_date.month != end_date.month:
            diff_months = end_date.month - start_date.month + 1
            if diff_months == 12:
                middle_str += '1年'
            else:
                middle_str += str(diff_months) + '月'
        if (start_date.day == start_date_first_day and end_date.day == end_date_last_day) and start_date.month == end_date.month:
                middle_str += '1月'

        if (start_date.day != start_date_first_day and end_date.day != end_date_last_day) and start_date.month == end_date.month:
                middle_str += str(end_date.day - start_date.day ) + '天'
        else:
            if ((start_date.day > start_date_first_day and start_date.day < start_date_last_day) and
                (end_date.day > end_date_first_day and end_date.day < end_date_last_day) ) or \
                    (start_date.day != start_date_first_day and end_date.day == end_date_first_day) or \
                    (start_date.day == start_date_last_day and end_date.day != end_date_first_day):
                is_one_month = start_date_last_day - start_date.day + 1 + end_date.day
                diff_days = 0
                diff_months = 0
                if is_one_month > end_date_last_day:
                    diff_months = end_date.month - start_date.month
                    diff_days = is_one_month - end_date_last_day
                if is_one_month == end_date_last_day:
                    diff_months = end_date.month - start_date.month
                if is_one_month < end_date_last_day:
                    diff_months = end_date.month - start_date.month -1
                    diff_days = is_one_month
                if diff_months > 0:
                    middle_str += str(diff_months) + '月'
                if diff_days > 0:
                    middle_str += str(diff_days) + '天'
            if start_date.day == start_date_first_day and end_date.day != end_date_last_day:
                diff_months = end_date.month - start_date.month
                diff_days = end_date.day
                if diff_months > 0:
                    middle_str += str(diff_months) + '月'
                middle_str += str(diff_days) + '天'
            if end_date.day == end_date_last_day and start_date.day != start_date_first_day:
                diff_months = end_date.month - start_date.month
                diff_days = start_date_last_day - start_date.day + 1
                if diff_months > 0:
                    middle_str += str(diff_months) + '月'
                if diff_days > 0:
                    middle_str += str(diff_days) + '天'
    else:
        #年份不相同直接取月份计算
        start_surplus_months = 12 - start_date.month
        end_date_before_months = end_date.month - 1
        sum_months = 0
        if end_date.year - start_date.year == 1:
            sum_months = start_surplus_months + end_date_before_months
        if end_date.year - start_date.year > 1:
            sum_months = start_surplus_months + end_date_before_months + (end_date.year - start_date.year -1) * 12
        if start_date.day == start_date_first_day and end_date.day == end_date_last_day:
            sum_months += 2
            diff_years = sum_months // 12
            diff_months = sum_months % 12
            if diff_years > 0:
                middle_str += str(diff_years) + '年'
            if diff_months > 0:
                middle_str += str(diff_months) + '月'
        if start_date.day == start_date_first_day and end_date.day != end_date_last_day:
            sum_months += 1
            diff_years = sum_months // 12
            diff_months = sum_months % 12
            if diff_years > 0:
                middle_str += str(diff_years) + '年'
            if diff_months > 0:
                middle_str += str(diff_months) + '月'
            middle_str += str(end_date.day) + '天'
        if start_date.day != start_date_first_day and end_date.day == end_date_last_day:
            sum_months += 1
            diff_years = sum_months // 12
            diff_months = sum_months % 12
            if diff_years > 0:
                middle_str += str(diff_years) + '年'
            if diff_months > 0:
                middle_str += str(diff_months) + '月'
            middle_str += str(start_date_last_day - start_date.day + 1) + '天'

        if ((start_date.day > start_date_first_day and start_date.day < start_date_last_day) and
            (end_date.day > end_date_first_day and end_date.day < end_date_last_day)) or \
                (start_date.day != start_date_first_day and end_date.day == end_date_first_day) or \
                (start_date.day == start_date_last_day and end_date.day != end_date_first_day):
            is_one_month = start_date_last_day - start_date.day + 1 + end_date.day
            if is_one_month >= end_date_last_day:
                sum_months += 1
                diff_years = sum_months // 12
                diff_months = sum_months % 12
                if diff_years > 0:
                    middle_str += str(diff_years) + '年'
                if diff_months > 0:
                    middle_str += str(diff_months) + '月'
                diff_days = is_one_month - end_date_last_day
                if diff_days > 0:
                    middle_str += str(diff_days) + '天'
            else:
                diff_years = sum_months // 12
                diff_months = sum_months % 12
                if diff_years > 0:
                    middle_str += str(diff_years) + '年'
                if diff_months > 0:
                    middle_str += str(diff_months) + '月'
                middle_str += str(is_one_month) + '天'
    return middle_str

print("日期间隔1===>",calcDateDiff(start_date,end_date))

def calcDateDiff2(start_date=datetime, end_date=datetime):
    result_str = ''
    diff_days = (end_date - start_date).days
    start_date_first_day,start_date_last_day = getMonthFirstDayAndLastDay(start_date.year,start_date.month)
    end_date_first_day, end_date_last_day = getMonthFirstDayAndLastDay(end_date.year, end_date.month)
    # 获取间隔年数:
    year = diff_days // 365
    if year > 0:
        result_str += str(year) + '年'
    # 间隔月数
    month = diff_days % 365 // 30
    if month > 0:
        result_str += str(month) + '月'
    # 间隔天数:
    day = diff_days % 365 % 30
    if day > 0:
        result_str += str(day) + '日'
    return result_str
print("日期间隔2===>",calcDateDiff2(start_date,end_date))

 


python 计算两个日期间隔年月日_第1张图片

你可能感兴趣的:(Python)