C中两个日期之间的差异

时间:2022-03-19 21:31:47

I'm a beginner in C.

我是C的初学者。

  • Is there any datatype for dates?
  • 日期是否有任何数据类型?

  • In C we have for working with time, is there one for dates too?
  • 在C我们有时间工作,还有一个日期吗?

  • How can I calculate difference between two dates?
  • 如何计算两个日期之间的差异?

8 个解决方案

#1


3  

Is there any datatype for dates?

日期是否有任何数据类型?

No, inbuilt datatype in C , you have to defined user-defined data type.

不,C中的内置数据类型,您必须定义用户定义的数据类型。

How can I calculate difference between two dates?

如何计算两个日期之间的差异?

You may try this:

你可以试试这个:

struct dt
{
  int dd;
  int mm;
  int yy;
};
typedef dt date;  

In main() you need to declare three variables for type data.
In following example today difference,
for example you wants to take difference between current date (c_date) and date of birth (dob)

在main()中,您需要为类型数据声明三个变量。在以下示例今天的差异中,例如,您希望区分当前日期(c_date)和出生日期(dob)

  date dob,c_date,today;

  if(c_date.dd>=dob.dd)
    today.dd = c_date.dd-dob.dd;
  else
  {
      c_date.dd+=30;
      c_date.mm-=1;
      today.dd = c_date.dd-dob.dd;
   }
   if(c_date.mm>=dob.mm)
     today.mm = c_date.mm-dob.mm;
   else
   {
      c_date.mm+=12;
      c_date.yy-=1;
      today.mm = c_date.dd-dob.mm;
   }
   today.yy = c_date.yy-dob.yy;

In today you have difference between two dates.

在今天你有两个日期之间的差异。

There is one more way: double difftime (time_t end, time_t beginning);
Read this answers:
1. How to compare two time stamp in format “Month Date hh:mm:ss"
2. How do you find the difference between two dates in hours, in C?

还有一种方法:double difftime(time_t end,time_t beginning);阅读以下答案:1。如何比较格式为“月份日期hh:mm:ss”的两个时间戳2.如何找到两个日期之间的差异,以小时为单位,用C表示?

#2


15  

Yes,the standard library C Time Library contains structures and functions you want.You can use struct tm to store date and difftime to get the difference.

是的,标准库C时间库包含您想要的结构和功能。您可以使用struct tm来存储日期和difftime以获得差异。

#3


4  

Is there any datatype for save dates?

是否有保存日期的数据类型?

No, although for dates in the range "now plus or minus a few decades" you could use time_t or struct tm containing the datetime at (for example) midnight on the relevant day. Alternatively you could look into a thing called the "Julian day": compute that and store it in whatever integer type you like.

不,虽然对于“现在加上或减去几十年”范围内的日期,您可以在相关日期的(例如)午夜使用包含日期时间的time_t或struct tm。或者你可以看一个叫做“Julian day”的东西:计算它并将它存储在你喜欢的任何整数类型中。

Is there any library for C too?

C也有图书馆吗?

The standard functions all relate to date/times rather than just dates: mktime, localtime, gmtime.

标准函数都与日期/时间有关,而不仅仅是日期:mktime,localtime,gmtime。

How can I calculate different between two date

如何计算两个日期之间的差异

Once you have it in a time_t you can subtract the two and divide by 86400. Watch out, though, since "midnight local time" on two different days might not be an exact multiple of 24 hours apart due to daylight savings changes.

一旦你有一个time_t,你可以减去两个并除以86400.但是,请注意,因为夏令时的变化,两天之后的“午夜当地时间”可能不会相隔24小时。

If you need a calendar that extends beyond the range of time_t on your implementation then you're basically on your own. If time_t is 64 bits then that's more than the age of the universe, but if time_t is 32 bits it's no good for history. Or pension planning, even. Historical applications have their own demands on calendars anyway (Julian calendar, calendars completely unrelated to Gregorian).

如果您需要一个超出time_t范围的日历,那么您基本上可以独立完成。如果time_t是64位,则超过宇宙的年龄,但如果time_t是32位,则对历史没有好处。或者甚至是养老金计划。无论如何,历史应用程序对日历都有自己的要求(儒略历,与格里高利完全无关的日历)。

#4


2  

You can create a struct named date having following fields

您可以创建名为date的结构,其中包含以下字段

typedef struct 
{
 int day;
 int month;
 int year;
}date;

It's just a blueprint what you want , now make and object of date and work accordingly. To find the difference ,write a function to take a difference between day month and year of the both stucts respectively.

这只是你想要的蓝图,现在制作和日期对象并相应地工作。要找到差异,请编写一个函数,分别对两个结构的日月和年份进行区分。

#5


2  

You have to define a date struct:

您必须定义日期结构:

typedef struct date {
    int day;
    int month;
    int year;
} Date;

And then define a simple date_compare() method:

然后定义一个简单的date_compare()方法:

int date_compare(Date *date1, Date *date2) {

    if (date1->year != date2->year)
        return (date1->year - date2->year);
    if (date1->month != date2->month)
        return (date1->month - date2->month);
    return (date1->day - date2->day);

}

#6


1  

The standard C library options for dates and times are pretty poor and loaded with caveats and limitations. If at all possible, use a library such as Gnome Lib which provides GDate and numerous useful date and time functions. This includes g_date_days_between() for getting the number of days between two dates.

日期和时间的标准C库选项非常差,并且存在警告和限制。如果可能的话,使用像Gnome Lib这样的库来提供GDate和许多有用的日期和时间函数。这包括g_date_days_between(),用于获取两个日期之间的天数。

The rest of this answer will restrict itself to the standard C library, but if you don't have to limit yourself to the standard, don't torture yourself. Dates are surprisingly hard.

本答案的其余部分将限制在标准C库中,但如果您不必限制自己的标准,请不要折磨自己。日期非常艰难。


Is there any datatype for dates?

日期是否有任何数据类型?

struct tm will serve. Just leave the hour, minutes, and seconds at 0.

struct tm将服务。只需将小时,分钟和秒留在0。

Simplest way to ensure all the fields of struct tm are properly populated is to use strptime.

确保struct tm的所有字段都正确填充的最简单方法是使用strptime。

struct tm date;
strptime( "2017-03-21", "%F", &date );

puts( asctime(&date) );  // Mon Mar 21 00:00:00 2017

But that's not a great way to store dates. It turns out it's better to use Julian Days (see below).

但这并不是存储日期的好方法。事实证明,使用Julian Days更好(见下文)。

In C we have for working with time, is there one for dates too?

在C我们有时间工作,还有一个日期吗?

If you're referring to time_t, that is also for date-times. It's the number of seconds since "the epoch" which is 1970-01-01 00:00:00 UTC on POSIX systems, but not necessarily others. Unfortunately its safe range is only 1970 to 2037, though any recent version of an operating system will have greatly expanded that range.

如果您指的是time_t,那也是日期时间。这是自POSIX系统上1970-01-01 00:00:00 UTC的“纪元”以来的秒数,但不一定是其他。不幸的是,它的安全范围仅为1970年至2037年,但任何最新版本的操作系统都将大大扩展该范围。

How can I calculate difference between two dates?

如何计算两个日期之间的差异?

Depends on what you want. If you want the number of seconds, you could convert the struct tm to time_t using mktime and then use difftime, but that's limited to the 1970-2037 safe range of time_t.

取决于你想要什么。如果你想要秒数,你可以使用mktime将struct tm转换为time_t然后使用difftime,但这仅限于1970-2037的time_t安全范围。

int main() {
    struct tm date1, date2;
    strptime( "2017-03-21", "%F", &date1 );
    strptime( "2018-01-20", "%F", &date2 );

    printf("%.0lf\n", difftime(mktime(&date1), mktime(&date2)));
}

If you want the number of days, you'd convert the dates into Julian days, the number of days since November 24, 4714 BC, and subtract. While that might seem ridiculous, this relatively simple formula takes advantage of calendar cycles and only uses integer math.

如果您想要天数,您可以将日期转换为儒略日,即自公元前4714年11月24日以来的天数,并减去。虽然这可能看起来很荒谬,但这个相对简单的公式利用了日历周期并且只使用整数数学。

// The formulas for a and m can be distilled down to these tables.
int Julian_A[12] = { 1, 1, 0 };
int Julian_M[12] = { 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int julian_day( struct tm *date ) {
    int a = Julian_A[date->tm_mon];
    int m = Julian_M[date->tm_mon];
    int y = date->tm_year + 1900 + 4800 - a;

    return date->tm_mday + ((153*m + 2) / 5) + 365*y + y/4 - y/100 + y/400 - 32045;
}

int main() {
    struct tm date1, date2;
    strptime( "2017-03-21", "%F", &date1 );
    strptime( "2018-01-20", "%F", &date2 );

    // 305 days
    printf("%d days\n", julian_day(&date2) - julian_day(&date1));
}

There are other simple formulas for converting between Julian Dates and calendar dates.

还有其他简单的公式可以在Julian日期和日历日期之间进行转换。

Getting diffs in years, months, and days is difficult because of the number of days in a month varies by month and year, and because it has to be normalized. For example, you wouldn't say 2 years, -1 months, 2 days. You'd say 1 year, 11 months, 29 days (or maybe 28, depends on the month). For this reason, do date math in Julian Days whenever possible.

由于一个月中的天数因月份和年份而异,并且因为必须进行标准化,因此难以在数年,数月和数天内获得差异。例如,你不会说2年,-1个月,2天。你会说1年,11个月,29天(或者28天,取决于月份)。因此,尽可能在Julian Days中进行日期数学运算。

To get an idea of what's involved, PHP implements this as date_diff. Have a look at the amount of C code required.

为了了解所涉及的内容,PHP将其实现为date_diff。看看所需的C代码量。

#7


0  

/*  Version 3 (better)
Date Difference between the two dates in days
like VBA function DateDiff("d", date1, date2)
in case of Gregorian. Same basic principle you
can translate in lot of other languages. This
is complete C code with date validity control.
*/

#include<stdio.h>

void main(){
  long d1,m1,y1,d2,m2,y2;
  printf("Enter first date day, month, year\n");
  scanf("%d%d%d",&d1,&m1,&y1);
  printf("Enter second date day, month, year\n");
  scanf("%d%d%d",&d2,&m2,&y2);
  if((IsValid(d1,m1,y1)==0)||(IsValid(d2,m2,y2)==0)){
    printf("Invalid date detected\n");
  }else{
    d1=DatDif(d1,m1,y1,d2,m2,y2);
    printf("\n\n Date difference is %d days\n",d1);
  }
}// end main

long DatDif(d1,m1,y1,d2,m2,y2)
{ long suma;
  suma=rbdug(d2,m2,y2) - rbdug(d1,m1,y1);
  if(y1 != y2){
    if(y1 < y2){
      suma+=Godn(y1,y2);
    }else{
      suma-=Godn(y2,y1);
    }
  }
  return(suma);
}// end DatDif

long Godn(yy1,yy2)
{ long jj,bb;
  bb=0;
  for(jj=yy1;jj<yy2;jj++){
    bb+=365;
    if(((((jj%400)==0)||((jj%100)!=0))
      &&((jj%4)==0))) bb+=1;
  }
  return(bb);
}// end Godn

//Day of the Year
long rbdug(d,m,y)
{ long a,r[13];
  r[1] = 0; r[2] = 31; r[3] = 59;
  r[4] = 90; r[5] = 120; r[6] = 151;
  r[7] = 181; r[8] = 212; r[9] = 243;
  r[10]= 273; r[11]= 304; r[12]= 334;
  a=r[m]+d;
  if(((((y%400)==0)||((y%100)!=0))
      &&((y%4)==0))&&(m>2)) a+=1;
  return(a);
}//end rbdug

//date validity
long IsValid(dd,mm,yy)
{ long v[13];
  if((0 < mm) && (mm < 13)){
    v[1] = 32; v[2] = 29; v[3] = 32;
    v[4] = 31; v[5] = 32; v[6] = 31;
    v[7] = 32; v[8] = 32; v[9] = 31;
    v[10]= 32; v[11]= 31; v[12]= 32;
    if(((((yy%400)==0)||((yy%100)!=0))
      &&((yy%4)==0))) v[2]+=1;
    if((0 < dd) && (dd < v[mm])){
      return(1);
    }else{
      return(0);
    }
  }else{
    return(0);
  }
}//end IsValid

#8


0  

/* Version 4 ( 100 % correct):
Proleptic Gregorian date difference in days.
Date Difference between the two dates in days
like VBA function DateDiff("d", date1, date2)
and better (without limitations of DateDiff)
in case of Gregorian. Same basic principle you
can translate in lot of other languages. This
is complete C code with date validity control.
*/

#include<stdio.h>

void main(){
  long d1,m1,y1,d2,m2,y2;
  printf("Enter first date day, month, year\n");
  scanf("%d%d%d",&d1,&m1,&y1);
  printf("Enter second date day, month, year\n");
  scanf("%d%d%d",&d2,&m2,&y2);
  if((IsValid(d1,m1,y1)==0)||(IsValid(d2,m2,y2)==0)){
    printf("Invalid date detected\n");
  }else{
    d1=DatDif(d1,m1,y1,d2,m2,y2);
    printf("\n\n Date difference is %d days\n",d1);
  }
}// end main

long DatDif(d1,m1,y1,d2,m2,y2)
{ long suma;
  suma=rbdug(d2,m2,y2) - rbdug(d1,m1,y1);
  if(y1 != y2){
    if(y1 < y2){
      suma+=Godn(y1,y2);
    }else{
      suma-=Godn(y2,y1);
    }
  }
  return(suma);
}// end DatDif

long Godn(yy1,yy2)
{ long jj,bb;
  bb=0;
  for(jj=yy1;jj<yy2;jj++){
    bb+=365;
    if(IsLeapG(jj)==1) bb+=1;
  }
  return(bb);
}// end Godn

//Day of the Year
long rbdug(d,m,y)
{ long a,r[13];
  r[1] = 0; r[2] = 31; r[3] = 59; r[4] = 90;
  r[5] = 120; r[6] = 151; r[7] = 181; r[8] = 212;
  r[9] = 243; r[10]= 273; r[11]= 304; r[12]= 334;
  a=r[m]+d;
  if((IsLeapG(y)==1)&&(m>2)) a+=1;
  return(a);
}//end rbdug

//date validity
long IsValid(dd,mm,yy)
{ long v[13];
  if((0 < mm) && (mm < 13)){
    v[1] = 32; v[2] = 29; v[3] = 32; v[4] = 31;
    v[5] = 32; v[6] = 31; v[7] = 32; v[8] = 32;
    v[9] = 31; v[10]= 32; v[11]= 31; v[12]= 32;
    if ((mm==2)&&(IsLeapG(yy)==1)) v[2]=30;
    if((0 < dd) && (dd < v[mm])){
      return(1);
    }else{
      return(0);
    }
  }else{
    return(0);
  }
}//end IsValid

//is leap year in Gregorian
long IsLeapG(yr){
  if(((((yr%400)==0)||((yr%100)!=0))&&((yr%4)==0))){
    return(1);
  }else{
    return(0);
  }
}//end IsLeapG

#1


3  

Is there any datatype for dates?

日期是否有任何数据类型?

No, inbuilt datatype in C , you have to defined user-defined data type.

不,C中的内置数据类型,您必须定义用户定义的数据类型。

How can I calculate difference between two dates?

如何计算两个日期之间的差异?

You may try this:

你可以试试这个:

struct dt
{
  int dd;
  int mm;
  int yy;
};
typedef dt date;  

In main() you need to declare three variables for type data.
In following example today difference,
for example you wants to take difference between current date (c_date) and date of birth (dob)

在main()中,您需要为类型数据声明三个变量。在以下示例今天的差异中,例如,您希望区分当前日期(c_date)和出生日期(dob)

  date dob,c_date,today;

  if(c_date.dd>=dob.dd)
    today.dd = c_date.dd-dob.dd;
  else
  {
      c_date.dd+=30;
      c_date.mm-=1;
      today.dd = c_date.dd-dob.dd;
   }
   if(c_date.mm>=dob.mm)
     today.mm = c_date.mm-dob.mm;
   else
   {
      c_date.mm+=12;
      c_date.yy-=1;
      today.mm = c_date.dd-dob.mm;
   }
   today.yy = c_date.yy-dob.yy;

In today you have difference between two dates.

在今天你有两个日期之间的差异。

There is one more way: double difftime (time_t end, time_t beginning);
Read this answers:
1. How to compare two time stamp in format “Month Date hh:mm:ss"
2. How do you find the difference between two dates in hours, in C?

还有一种方法:double difftime(time_t end,time_t beginning);阅读以下答案:1。如何比较格式为“月份日期hh:mm:ss”的两个时间戳2.如何找到两个日期之间的差异,以小时为单位,用C表示?

#2


15  

Yes,the standard library C Time Library contains structures and functions you want.You can use struct tm to store date and difftime to get the difference.

是的,标准库C时间库包含您想要的结构和功能。您可以使用struct tm来存储日期和difftime以获得差异。

#3


4  

Is there any datatype for save dates?

是否有保存日期的数据类型?

No, although for dates in the range "now plus or minus a few decades" you could use time_t or struct tm containing the datetime at (for example) midnight on the relevant day. Alternatively you could look into a thing called the "Julian day": compute that and store it in whatever integer type you like.

不,虽然对于“现在加上或减去几十年”范围内的日期,您可以在相关日期的(例如)午夜使用包含日期时间的time_t或struct tm。或者你可以看一个叫做“Julian day”的东西:计算它并将它存储在你喜欢的任何整数类型中。

Is there any library for C too?

C也有图书馆吗?

The standard functions all relate to date/times rather than just dates: mktime, localtime, gmtime.

标准函数都与日期/时间有关,而不仅仅是日期:mktime,localtime,gmtime。

How can I calculate different between two date

如何计算两个日期之间的差异

Once you have it in a time_t you can subtract the two and divide by 86400. Watch out, though, since "midnight local time" on two different days might not be an exact multiple of 24 hours apart due to daylight savings changes.

一旦你有一个time_t,你可以减去两个并除以86400.但是,请注意,因为夏令时的变化,两天之后的“午夜当地时间”可能不会相隔24小时。

If you need a calendar that extends beyond the range of time_t on your implementation then you're basically on your own. If time_t is 64 bits then that's more than the age of the universe, but if time_t is 32 bits it's no good for history. Or pension planning, even. Historical applications have their own demands on calendars anyway (Julian calendar, calendars completely unrelated to Gregorian).

如果您需要一个超出time_t范围的日历,那么您基本上可以独立完成。如果time_t是64位,则超过宇宙的年龄,但如果time_t是32位,则对历史没有好处。或者甚至是养老金计划。无论如何,历史应用程序对日历都有自己的要求(儒略历,与格里高利完全无关的日历)。

#4


2  

You can create a struct named date having following fields

您可以创建名为date的结构,其中包含以下字段

typedef struct 
{
 int day;
 int month;
 int year;
}date;

It's just a blueprint what you want , now make and object of date and work accordingly. To find the difference ,write a function to take a difference between day month and year of the both stucts respectively.

这只是你想要的蓝图,现在制作和日期对象并相应地工作。要找到差异,请编写一个函数,分别对两个结构的日月和年份进行区分。

#5


2  

You have to define a date struct:

您必须定义日期结构:

typedef struct date {
    int day;
    int month;
    int year;
} Date;

And then define a simple date_compare() method:

然后定义一个简单的date_compare()方法:

int date_compare(Date *date1, Date *date2) {

    if (date1->year != date2->year)
        return (date1->year - date2->year);
    if (date1->month != date2->month)
        return (date1->month - date2->month);
    return (date1->day - date2->day);

}

#6


1  

The standard C library options for dates and times are pretty poor and loaded with caveats and limitations. If at all possible, use a library such as Gnome Lib which provides GDate and numerous useful date and time functions. This includes g_date_days_between() for getting the number of days between two dates.

日期和时间的标准C库选项非常差,并且存在警告和限制。如果可能的话,使用像Gnome Lib这样的库来提供GDate和许多有用的日期和时间函数。这包括g_date_days_between(),用于获取两个日期之间的天数。

The rest of this answer will restrict itself to the standard C library, but if you don't have to limit yourself to the standard, don't torture yourself. Dates are surprisingly hard.

本答案的其余部分将限制在标准C库中,但如果您不必限制自己的标准,请不要折磨自己。日期非常艰难。


Is there any datatype for dates?

日期是否有任何数据类型?

struct tm will serve. Just leave the hour, minutes, and seconds at 0.

struct tm将服务。只需将小时,分钟和秒留在0。

Simplest way to ensure all the fields of struct tm are properly populated is to use strptime.

确保struct tm的所有字段都正确填充的最简单方法是使用strptime。

struct tm date;
strptime( "2017-03-21", "%F", &date );

puts( asctime(&date) );  // Mon Mar 21 00:00:00 2017

But that's not a great way to store dates. It turns out it's better to use Julian Days (see below).

但这并不是存储日期的好方法。事实证明,使用Julian Days更好(见下文)。

In C we have for working with time, is there one for dates too?

在C我们有时间工作,还有一个日期吗?

If you're referring to time_t, that is also for date-times. It's the number of seconds since "the epoch" which is 1970-01-01 00:00:00 UTC on POSIX systems, but not necessarily others. Unfortunately its safe range is only 1970 to 2037, though any recent version of an operating system will have greatly expanded that range.

如果您指的是time_t,那也是日期时间。这是自POSIX系统上1970-01-01 00:00:00 UTC的“纪元”以来的秒数,但不一定是其他。不幸的是,它的安全范围仅为1970年至2037年,但任何最新版本的操作系统都将大大扩展该范围。

How can I calculate difference between two dates?

如何计算两个日期之间的差异?

Depends on what you want. If you want the number of seconds, you could convert the struct tm to time_t using mktime and then use difftime, but that's limited to the 1970-2037 safe range of time_t.

取决于你想要什么。如果你想要秒数,你可以使用mktime将struct tm转换为time_t然后使用difftime,但这仅限于1970-2037的time_t安全范围。

int main() {
    struct tm date1, date2;
    strptime( "2017-03-21", "%F", &date1 );
    strptime( "2018-01-20", "%F", &date2 );

    printf("%.0lf\n", difftime(mktime(&date1), mktime(&date2)));
}

If you want the number of days, you'd convert the dates into Julian days, the number of days since November 24, 4714 BC, and subtract. While that might seem ridiculous, this relatively simple formula takes advantage of calendar cycles and only uses integer math.

如果您想要天数,您可以将日期转换为儒略日,即自公元前4714年11月24日以来的天数,并减去。虽然这可能看起来很荒谬,但这个相对简单的公式利用了日历周期并且只使用整数数学。

// The formulas for a and m can be distilled down to these tables.
int Julian_A[12] = { 1, 1, 0 };
int Julian_M[12] = { 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int julian_day( struct tm *date ) {
    int a = Julian_A[date->tm_mon];
    int m = Julian_M[date->tm_mon];
    int y = date->tm_year + 1900 + 4800 - a;

    return date->tm_mday + ((153*m + 2) / 5) + 365*y + y/4 - y/100 + y/400 - 32045;
}

int main() {
    struct tm date1, date2;
    strptime( "2017-03-21", "%F", &date1 );
    strptime( "2018-01-20", "%F", &date2 );

    // 305 days
    printf("%d days\n", julian_day(&date2) - julian_day(&date1));
}

There are other simple formulas for converting between Julian Dates and calendar dates.

还有其他简单的公式可以在Julian日期和日历日期之间进行转换。

Getting diffs in years, months, and days is difficult because of the number of days in a month varies by month and year, and because it has to be normalized. For example, you wouldn't say 2 years, -1 months, 2 days. You'd say 1 year, 11 months, 29 days (or maybe 28, depends on the month). For this reason, do date math in Julian Days whenever possible.

由于一个月中的天数因月份和年份而异,并且因为必须进行标准化,因此难以在数年,数月和数天内获得差异。例如,你不会说2年,-1个月,2天。你会说1年,11个月,29天(或者28天,取决于月份)。因此,尽可能在Julian Days中进行日期数学运算。

To get an idea of what's involved, PHP implements this as date_diff. Have a look at the amount of C code required.

为了了解所涉及的内容,PHP将其实现为date_diff。看看所需的C代码量。

#7


0  

/*  Version 3 (better)
Date Difference between the two dates in days
like VBA function DateDiff("d", date1, date2)
in case of Gregorian. Same basic principle you
can translate in lot of other languages. This
is complete C code with date validity control.
*/

#include<stdio.h>

void main(){
  long d1,m1,y1,d2,m2,y2;
  printf("Enter first date day, month, year\n");
  scanf("%d%d%d",&d1,&m1,&y1);
  printf("Enter second date day, month, year\n");
  scanf("%d%d%d",&d2,&m2,&y2);
  if((IsValid(d1,m1,y1)==0)||(IsValid(d2,m2,y2)==0)){
    printf("Invalid date detected\n");
  }else{
    d1=DatDif(d1,m1,y1,d2,m2,y2);
    printf("\n\n Date difference is %d days\n",d1);
  }
}// end main

long DatDif(d1,m1,y1,d2,m2,y2)
{ long suma;
  suma=rbdug(d2,m2,y2) - rbdug(d1,m1,y1);
  if(y1 != y2){
    if(y1 < y2){
      suma+=Godn(y1,y2);
    }else{
      suma-=Godn(y2,y1);
    }
  }
  return(suma);
}// end DatDif

long Godn(yy1,yy2)
{ long jj,bb;
  bb=0;
  for(jj=yy1;jj<yy2;jj++){
    bb+=365;
    if(((((jj%400)==0)||((jj%100)!=0))
      &&((jj%4)==0))) bb+=1;
  }
  return(bb);
}// end Godn

//Day of the Year
long rbdug(d,m,y)
{ long a,r[13];
  r[1] = 0; r[2] = 31; r[3] = 59;
  r[4] = 90; r[5] = 120; r[6] = 151;
  r[7] = 181; r[8] = 212; r[9] = 243;
  r[10]= 273; r[11]= 304; r[12]= 334;
  a=r[m]+d;
  if(((((y%400)==0)||((y%100)!=0))
      &&((y%4)==0))&&(m>2)) a+=1;
  return(a);
}//end rbdug

//date validity
long IsValid(dd,mm,yy)
{ long v[13];
  if((0 < mm) && (mm < 13)){
    v[1] = 32; v[2] = 29; v[3] = 32;
    v[4] = 31; v[5] = 32; v[6] = 31;
    v[7] = 32; v[8] = 32; v[9] = 31;
    v[10]= 32; v[11]= 31; v[12]= 32;
    if(((((yy%400)==0)||((yy%100)!=0))
      &&((yy%4)==0))) v[2]+=1;
    if((0 < dd) && (dd < v[mm])){
      return(1);
    }else{
      return(0);
    }
  }else{
    return(0);
  }
}//end IsValid

#8


0  

/* Version 4 ( 100 % correct):
Proleptic Gregorian date difference in days.
Date Difference between the two dates in days
like VBA function DateDiff("d", date1, date2)
and better (without limitations of DateDiff)
in case of Gregorian. Same basic principle you
can translate in lot of other languages. This
is complete C code with date validity control.
*/

#include<stdio.h>

void main(){
  long d1,m1,y1,d2,m2,y2;
  printf("Enter first date day, month, year\n");
  scanf("%d%d%d",&d1,&m1,&y1);
  printf("Enter second date day, month, year\n");
  scanf("%d%d%d",&d2,&m2,&y2);
  if((IsValid(d1,m1,y1)==0)||(IsValid(d2,m2,y2)==0)){
    printf("Invalid date detected\n");
  }else{
    d1=DatDif(d1,m1,y1,d2,m2,y2);
    printf("\n\n Date difference is %d days\n",d1);
  }
}// end main

long DatDif(d1,m1,y1,d2,m2,y2)
{ long suma;
  suma=rbdug(d2,m2,y2) - rbdug(d1,m1,y1);
  if(y1 != y2){
    if(y1 < y2){
      suma+=Godn(y1,y2);
    }else{
      suma-=Godn(y2,y1);
    }
  }
  return(suma);
}// end DatDif

long Godn(yy1,yy2)
{ long jj,bb;
  bb=0;
  for(jj=yy1;jj<yy2;jj++){
    bb+=365;
    if(IsLeapG(jj)==1) bb+=1;
  }
  return(bb);
}// end Godn

//Day of the Year
long rbdug(d,m,y)
{ long a,r[13];
  r[1] = 0; r[2] = 31; r[3] = 59; r[4] = 90;
  r[5] = 120; r[6] = 151; r[7] = 181; r[8] = 212;
  r[9] = 243; r[10]= 273; r[11]= 304; r[12]= 334;
  a=r[m]+d;
  if((IsLeapG(y)==1)&&(m>2)) a+=1;
  return(a);
}//end rbdug

//date validity
long IsValid(dd,mm,yy)
{ long v[13];
  if((0 < mm) && (mm < 13)){
    v[1] = 32; v[2] = 29; v[3] = 32; v[4] = 31;
    v[5] = 32; v[6] = 31; v[7] = 32; v[8] = 32;
    v[9] = 31; v[10]= 32; v[11]= 31; v[12]= 32;
    if ((mm==2)&&(IsLeapG(yy)==1)) v[2]=30;
    if((0 < dd) && (dd < v[mm])){
      return(1);
    }else{
      return(0);
    }
  }else{
    return(0);
  }
}//end IsValid

//is leap year in Gregorian
long IsLeapG(yr){
  if(((((yr%400)==0)||((yr%100)!=0))&&((yr%4)==0))){
    return(1);
  }else{
    return(0);
  }
}//end IsLeapG

相关文章