计算闰年中两个日期之间的天数

时间:2021-02-08 21:29:00

Given two dates, what is the best method to calculate the number of days between those two dates that fall in a leap year.

给定两个日期,计算闰年中这两个日期之间的天数的最佳方法是什么。

For example if d1 = 12/1/2007 and d2 = 1/31/2008 then the total number of days between d1 and d2 would be 62 and the number of days that fall in a leap year would be 31.

例如,如果d1 = 12/1/2007且d2 = 1/31/2008,则d1和d2之间的总天数将为62,而闰年中的天数将为31。

Another example is if d1 = 12/1/2007 and d2 = 6/30/2012 then the total number of days between d1 and d2 would be 1674 and the number of days that fall in a leap year would be 548.

另一个例子是如果d1 = 12/1/2007且d2 = 6/30/2012,那么d1和d2之间的总天数将是1674,并且闰年中的天数将是548。

I already have function to calculate if a specific year is a leap year and and a function to calculate the number of days between two dates.

我已经有了计算特定年份是否为闰年的函数,以及计算两个日期之间的天数的函数。

If anyone has such a algorithm in Delphi (Pascal) or C/C++/C# that would be greatly appreciated. Any suggestions and assistance would be great.

如果有人在Delphi(Pascal)或C / C ++ / C#中有这样的算法,那将非常感激。任何建议和帮助都会很棒。

3 个解决方案

#1


Here's my pseudo code version using your functions for - is_leap_year, days_between. As a commenter noted, these are tricky functions to write correctly.

这是我的伪代码版本,使用你的函数 - is_leap_year,days_between。正如评论者所指出的,这些是正确编写的棘手功能。

int leap_year_days_between(Date d1, Date d2) {

   if (d1.year == d2.year) {
       if (is_leap_year(d1.year) { return days_between(d1,d2); } 
       else { return 0; }
    }
    else {
      Date last_day_in_year(12, 31, d1.year);
      int count=0;
      Date tmp = d1;
      while (tmp.year < d2.year) {
         if ( is_leap_year(tmp.year) ) {
             count += days_between(tmp,last_day_in_year);
          }
          tmp = (1, 1, tmp.year+1);
      }
      if ( is_leap_year(d2.year) ) {
         count += days_between(tmp, d2);
      }

     }
}

#2


The solution is in python, and it shouldn't be hard to convert to any other language.

解决方案是在python中,转换为任何其他语言应该不难。

def isLeapYear(year):
    if year%4 == 0:
        if year%100 == 0:
            if year%400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    cumDays = [0,31,59,90,120,151,181,212,243,273,304,334] #cumulative Days by month
    leapcumDays = [0,31,60,91,121,152,182,213,244,274,305,335] # Cumulative Days by month for leap year
    totdays = 0
    if year1 == year2:
        if isLeapYear(year1):
            return (leapcumDays[month2-1] + day2) - (leapcumDays[month1-1] + day1)
        else:
            return (cumDays[month2-1] + day2) - (cumDays[month1-1] + day1)

    if isLeapYear(year1):
        totdays = totdays + 366 - (leapcumDays[month1-1] + day1)
    else:
        totdays = totdays + 365 - (cumDays[month1-1] + day1)

    year = year1 + 1
    while year < year2:
        if isLeapYear(year):
            totdays = totdays + 366
        else:
            totdays = totdays + 365
        year = year + 1

    if isLeapYear(year2):
        totdays = totdays + (leapcumDays[month2-1] + day2)
    else:
        totdays = totdays + (cumDays[month2-1] + day2)
    return totdays

#3


A naive approach would be:

一个天真的方法是:

Check your start year. If it's a leap year, count the number of days from your current day to December 31 (inclusive). If not, until your starting year equals your ending year, increment the year by 1. Then, check the year. If it is a leap year, start counting days, if not increment the year. Once the current year and ending year are the same, then check to see if the current (== ending) year is a leap year. If it is, count days in months from January to the ending month, otherwise break the algorithm. Once your current month is your ending month, count your days.

检查你的开始年份。如果是闰年,请计算从当天到12月31日(含)的天数。如果没有,直到您的起始年份等于结束年份,将年份增加1.然后,检查年份。如果是闰年,则开始计算天数,如果不是增加年份。一旦当前年份和结束年份相同,则检查当前(==结束)年份是否为闰年。如果是,则计算从1月到结束月份的月数,否则会中断算法。一旦您的当月是结束月份,请计算您的日期。

#1


Here's my pseudo code version using your functions for - is_leap_year, days_between. As a commenter noted, these are tricky functions to write correctly.

这是我的伪代码版本,使用你的函数 - is_leap_year,days_between。正如评论者所指出的,这些是正确编写的棘手功能。

int leap_year_days_between(Date d1, Date d2) {

   if (d1.year == d2.year) {
       if (is_leap_year(d1.year) { return days_between(d1,d2); } 
       else { return 0; }
    }
    else {
      Date last_day_in_year(12, 31, d1.year);
      int count=0;
      Date tmp = d1;
      while (tmp.year < d2.year) {
         if ( is_leap_year(tmp.year) ) {
             count += days_between(tmp,last_day_in_year);
          }
          tmp = (1, 1, tmp.year+1);
      }
      if ( is_leap_year(d2.year) ) {
         count += days_between(tmp, d2);
      }

     }
}

#2


The solution is in python, and it shouldn't be hard to convert to any other language.

解决方案是在python中,转换为任何其他语言应该不难。

def isLeapYear(year):
    if year%4 == 0:
        if year%100 == 0:
            if year%400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    cumDays = [0,31,59,90,120,151,181,212,243,273,304,334] #cumulative Days by month
    leapcumDays = [0,31,60,91,121,152,182,213,244,274,305,335] # Cumulative Days by month for leap year
    totdays = 0
    if year1 == year2:
        if isLeapYear(year1):
            return (leapcumDays[month2-1] + day2) - (leapcumDays[month1-1] + day1)
        else:
            return (cumDays[month2-1] + day2) - (cumDays[month1-1] + day1)

    if isLeapYear(year1):
        totdays = totdays + 366 - (leapcumDays[month1-1] + day1)
    else:
        totdays = totdays + 365 - (cumDays[month1-1] + day1)

    year = year1 + 1
    while year < year2:
        if isLeapYear(year):
            totdays = totdays + 366
        else:
            totdays = totdays + 365
        year = year + 1

    if isLeapYear(year2):
        totdays = totdays + (leapcumDays[month2-1] + day2)
    else:
        totdays = totdays + (cumDays[month2-1] + day2)
    return totdays

#3


A naive approach would be:

一个天真的方法是:

Check your start year. If it's a leap year, count the number of days from your current day to December 31 (inclusive). If not, until your starting year equals your ending year, increment the year by 1. Then, check the year. If it is a leap year, start counting days, if not increment the year. Once the current year and ending year are the same, then check to see if the current (== ending) year is a leap year. If it is, count days in months from January to the ending month, otherwise break the algorithm. Once your current month is your ending month, count your days.

检查你的开始年份。如果是闰年,请计算从当天到12月31日(含)的天数。如果没有,直到您的起始年份等于结束年份,将年份增加1.然后,检查年份。如果是闰年,则开始计算天数,如果不是增加年份。一旦当前年份和结束年份相同,则检查当前(==结束)年份是否为闰年。如果是,则计算从1月到结束月份的月数,否则会中断算法。一旦您的当月是结束月份,请计算您的日期。