繁体   English   中英

如何在 python 中找到两个日期之间的月份? 不基于仅基于月值的 30 天?

[英]how to find the months between two dates in python? Not based on 30 days based on month value only?

例如:

输入:开始日期: 31-07-2022结束日期: 01-08-2022

Output: 2 个月

开始日期: 01-01-2022结束日期: 01-01-2023

Output: 13 个月

您还需要提到开始日期的月份也计为 1。

def month_counter(start_date, end_date):
    start_date = list(map(int, start_date.split("-")))
    end_date = list(map(int, end_date.split("-")))
    
    year_counter =  (end_date[2] - start_date[2]) * 12
    month_counter = end_date[1] - start_date[1] + 1
    return f"{year_counter + month_counter} Months"
print(month_counter("31-07-2022", "01-08-2022"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM