The difference between two dates in C

I am new to C.

  • Is there any data type for dates?
  • In C, we work with time, are there any dates?
  • How can I calculate the difference between two dates?
+7
source share
8 answers

Is there any data type for dates?

No, the built-in data type in C , you must define a user-defined data type.

How can I calculate the difference between two dates?

You can try the following:

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

In main (), you need to declare three variables for the data type.
In the following example, the difference is today ,
for example, you want to distinguish between current date ( c_date ) and date of birth ( 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 a difference between two dates.

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

+2
source

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

+15
source

Is there any data type for storing dates?

No, although for dates in the “now plus or minus several decades” range, you can use time_t or struct tm containing the date and time at (for example) midnight on the corresponding day. Alternatively, you could learn a thing called "Julian day": compute it and save it in whatever type you like.

Is there a library for C?

Standard functions all relate to date / time, and not just to dates: mktime , localtime , gmtime .

How can I calculate the difference between two dates

Once you have this in time_t , you can subtract two and divide by 86400. However, given that the "midnight local time" on two different days may not be a multiple of 24 hours a day due to daylight savings.

If you need a calendar that goes beyond the time_t range in your implementation, then you are basically on your own. If time_t is 64 bits, then this is greater than the age of the universe, but if time_t is 32 bits, this is not useful for history. Or even retirement planning. Historical applications have all their requirements for calendars in any case (Julian calendar, calendars completely unrelated to the Gregorian).

+4
source

You can create a struct named date with the following fields

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

This is just the project you want, now make a date object and work accordingly. To find the difference, write a function to get the difference between the day month and year both columns, respectively.

+2
source

You must define the date structure:

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

And then define a simple date_compare() method:

 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); } 
+2
source

The standard C library features 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 many useful date and time functions . This includes g_date_days_between() to get the number of days between two dates.

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


Is there any data type for dates?

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

The easiest way to ensure that all struct tm fields are populated correctly is to use strptime .

 struct tm date; strptime( "2017-03-21", "%F", &date ); puts( asctime(&date) ); // Mon Mar 21 00:00:00 2017 

But this is a great way to store dates. It turns out that it is better to use Julian days (see below).

In C, we deal with time, are there any dates?

If you mean time_t , this also applies to dates. This is the number of seconds since the "era" , which is 1970-01-01 00:00:00 UTC on POSIX systems, but not necessarily others. Unfortunately, its safe range is only from 1970 to 2037, although any recent version of the operating system will greatly expand this range.

How can I calculate the difference between two dates?

Depends on what you want. If you want the number of seconds, you can convert struct tm to time_t using mktime and then use difftime , but this is limited by the valid time_t range from 1970-2037.

 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 convert the dates to Julian days , the number of days from November 24, 4714 BC and subtract. Although this may seem ridiculous, this relatively simple formula takes advantage of calendar cycles and uses only mathematical math.

 // 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 yuan dates and calendar dates.

Obtaining differences by year, month, and day is difficult because the number of days per month varies depending on the month and year and because it needs to be normalized. For example, you would not say 2 years, -1 month, 2 days. You say 1 year, 11 months, 29 days (or maybe 28, depends on the month). For this reason, do the math date on Julian Days whenever possible.

To get an idea of ​​what is involved, PHP implements this as date_diff . See the amount of C code required .

+1
source
 /* 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 
0
source
 /* 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 
0
source

All Articles