[英]Calculate the number of days between two dates in C – Convert date to days
[英]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.