繁体   English   中英

如何计算两个不同日期之间的天数

[英]how to calculate the number of days between two different dates

我正在使用一个公式来计算日历中两个不同日期之间的天数,而得到的答案与我正在查看的示例没有相同的答案。 我正在尝试遵循以下公式:

N = 1461 xf(年,月)/ 4 + g(月)/ 5 +天

如果f(年,月)= -1年(如果月<= 2),否则返回相同的值。 g(month)= month + 13,如果month <=2。否则为month + 1。

这是我实现上述公式的方法:

#include <stdio.h>
#define DEBUG

  struct date
    {
      int month ;
      int day ;
      int year ;
    };
  int een (struct date _date)
    {
      int n;
      #ifdef DEBUG
      printf("inside of een function - here is what is in date %i/%i/%i \n", _date.month, _date.day, _date.year);
      #endif
      n = 1461 * f(_date.year, _date.month) / 4 + 153 * g(_date.month) / 5 + _date.day;
      #ifdef DEBUG
      printf("Here is the value of n %i \n", n);
      #endif
      return n;
    }
  int f(int year, int month)
    {
      if (month <= 2)
      year = (year - 1);
      return year;
    }
   int g(int month)
    {
      if (month <= 2)
        month = month + 13;
      else
      month = month +1;
      return month;
    }
int main (void)
{
  struct date first_Day, second_Day;
    int days;
  int een (struct date _date);
  printf ("Enter the first date (mm dd yyyy) :");
  scanf ("%i%i%i", &first_Day.month, &first_Day.day, &first_Day.year);
  #ifdef DEBUG
  printf("Here is what you entered as the first date %i/%i/%i \n", first_Day.month, first_Day.day, first_Day.year);
  #endif
  printf ("Enter Today's date (mm dd yyyy) :");
  scanf ("%i%i%i", &second_Day.month, &second_Day.day, &second_Day.year);
  days = een (second_Day) - een (first_Day);
  printf("Here are the number of days between the dates you put in  %i \n ", days);
}

我正在查看的一个示例计算2004年8月8日的N如下:

N = 1461 xf(2004,8)/ 4 + 153 xg(8)/ 5 + 3

我知道上面的代码有一些问题。 我也不知道他/她在8月8日如何获得3分。 我希望有人以前使用过此公式,并且他们可以解释上面的示例是否错误或需要其他解释。 我看过其他有关这种类型代码的文章,他们没有讨论我的问题。 我也研究了其他站点,但是找不到答案。 我知道timeanddate.com有一个计算器,根据它,我上面的代码不起作用。

我将再等一会儿,看看是否有人之前曾使用过此公式,并获得了一些反馈。 然后,我将在g()函数中使用12。

根据作者,这是两个示例计算。 2004年8月8日,他得到:

N= 1461 x f(2004, 8) / 4 + 153 x g(8) / 5 + 3 
= (1461 x 2004) / 4 + (153 x 9) / 5 + 3 
= 2,927,844 / 4 + (153 x 9) / 5 + 3
= 731,961 + 275 + 3
= 732,239

在2005年2月22日,他获得:

1461 x f(2005, 2) / 4 + 153 x g(2) / 5 + 21
(1461 x 2004) / 4 + 2295 / 5 + 21 
731961 + 459 + 21 
732,441

之间的天数:731,441-732,239 = 202

坦白说,我不确定他如何得出上述样本中“ day”值的3和21。

我还在这里检查了答案,它说199天。 我想我可以确定上述日期的天变量是如何计算的。 我想会有一些差异,这取决于每年如何处理额外的四个小时。

在Windows中,您可以执行以下操作:

__int64  since_ft, until_ft;
SYSTEMTIME since, until;

SystemTimeToFileTime(&since, (LPFILETIME)&since_ft);
SystemTimeToFileTime(&until, (LPFILETIME)&until_ft);
int days = (int)((until_ft - since_ft) / (__int64)864000000000);

我添加了其他。 对于2004年8月8日和2005年2月22日之间的差异,我得到了198天而不是202天的答案。 但是,这仍然是错误的。

实际上,您的代码可能是正确的。

作为交叉检查,下面是一种使用mktime [ 旁注:处理leap年]的方法来获取绝对时间,求和并除以天数。 这类似于大卫的建议。

与您的输出一样,其输出为198 我认为这非常准确,但是即使出现一个错误的错误,仍然可以得到197-199的答案。 IMO 202太高了。

#include <stdio.h>
#include <string.h>
#include <time.h>

time_t
timeof(int mon,int day,int yr)
{
    struct tm tm;
    time_t tod;

    memset(&tm,0,sizeof(tm));

    tm.tm_year = yr - 1900;
    tm.tm_mon = mon - 1;
    tm.tm_mday = day;

    // minimize funny business with DST crossover or [gag] leap seconds
    tm.tm_hour = 1;

    tod = mktime(&tm);

    return tod;
}

int
main(void)
{
    time_t t1;
    time_t t2;
    time_t td;

    t1 = timeof(8,8,2004);
    t2 = timeof(2,22,2005);

    td = t2 - t1;
    td /= (24 * 60 * 60);

    printf("%ld\n",td);

    return 0;
}

顺便说一句,您可以使用GNU date实用程序快速检查日期算术:

$ date -d 'August 8 2004 + 198 days'
Tue Feb 22 00:00:00 EST 2005

所以我坚持用198作为答案。

暂无
暂无

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

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