You are here:

C/Computing difference between two dates

Advertisement


Ferrari wrote at 2007-12-20 04:32:33
This is the best program for caluclating diff between dates....





#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>



void stotm(struct tm *p, const char *s)

{

int i;

//struct tm blank = {0};

//*p = blank;



for(i = 0; i < 4; i++)

{

p->tm_year *= 10;

p->tm_year += *s++ - '0';

}

p->tm_year -= 1900;

for(i = 0; i < 2; i++)

{

p->tm_mon *= 10;

p->tm_mon += *s++ - '0';

}

--p->tm_mon;

p->tm_mday = 10 * (*s++ - '0');

p->tm_mday += *s++ - '0';

}



void calcdiff(const char *old, const char *new1)

{

struct tm t1 = {0};

struct tm t2 = {0};

time_t tt1 = {0}; //time_t arithmetic type that represents times

time_t tt2 = {0};



stotm(&t1, old);

stotm(&t2, new1);

tt1 = mktime(&t1);

tt2 = mktime(&t2);

if(tt1 != -1 && tt2 != -1)

{

long day = 0;

double d = difftime(tt1, tt2);

if(d < 0) d *= -1.0;

day = d / 86400;

printf("%ld day%s\n", day, day == 1 ? "" : "s");

}

else

{

fprintf(stderr, "bad date format: %ld %ld\n", (long)tt1, (long)tt2);

}

}



int main(int argc, char *argv[])

{

if(argc < 3 || strlen(argv[1]) < 8 || strlen(argv[2]) < 8)

{

fprintf(stderr, "Learn.\n");

}

else

{

if(strcmp(argv[1], argv[2]) < 0)

{

calcdiff(argv[1], argv[2]);

}

else

{

calcdiff(argv[2], argv[1]);

}

}

return 0;

}  


harish wrote at 2009-11-28 08:16:38
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

{

 int year(char *date_string);

 int month(char *date_string);

 int date(char *date_string);

 int validDate(char *date_string);

 int number_of_days_till_now(char *x);

 int number_of_days_in_the_year(int x);

 char d1[11],d2[11];

 char *p1=d1,*p2=d2;

 int sum=0,sum1=0,sum2=0,diff=0,yd,i=0;

 int ns=0;

 clrscr();

 printf("\nenter date1(dd/mm/yyyy):");

 scanf("%s",d1);

 if(validDate(p1))

 {

   printf("date1 is valid");

 }

 else

 {

   printf("date1 is invalid");

 }

 printf("\nenter date2(dd/mm/yyyy):");

 scanf("%s",d2);

 if(validDate(p2))

 {

   printf("date2 is valid");

 }

 else

 {

   printf("date2 is invalid");

 }

 yd=year(p2)-year(p1);

 if(yd>1)

 {

   for(i=(year(p1)+1);i<year(p2);i++)

   {

     sum+=number_of_days_in_the_year(i);

   }

 sum1=number_of_days_till_now(p2);

 sum2=number_of_days_in_the_year(year(p1))-number_of_days_till_now(p1);

 diff=sum+sum1+sum2;

 }

 else if(yd==1)

 {

 sum=0;

 sum1=number_of_days_till_now(p2);

 sum2=number_of_days_in_the_year(year(p1))-number_of_days_till_now(p1);

 diff=sum+sum1+sum2;

 

 }

 else if(yd==0)

 {

   sum=0;

   sum1=number_of_days_till_now(p2);

   sum2=number_of_days_till_now(p1);

   diff=sum+sum1-sum2;



 }

 printf("\nDifference between the given dates(in no.of days)=%d",diff);

 getch();

}

int validDate(char *x)

{

 int d,m,y,h,mi,s;

 char date[3],month[3],year[5],hours[3],minutes[3],seconds[3];

 date[0]=x[0];

 date[1]=x[1];

 date[2]='\0';

 d=atoi(date);

 month[0]=x[3];

 month[1]=x[4];

 month[2]='\0';

 m=atoi(month);

 year[0]=x[6];

 year[1]=x[7];

 year[2]=x[8];

 year[3]=x[9];

 year[4]='\0';

 y=atoi(year);

 hours[0]=x[10];

 hours[1]=x[11];

 hours[2]='\0';

 h=atoi(hours);

 minutes[0]=x[13];

 minutes[1]=x[14];

 minutes[2]='\0';

 mi=atoi(minutes);

 seconds[0]=x[16];

 seconds[1]=x[17];

 seconds[2]='\0';

 s=atoi(seconds);

 if(m>12||m<1)

 {

   return 0;

 }

 switch(m)

 {

   case 1:

        if(d>31||d<1)

        {

     return 0;

        }

        break;

   case 2:

        if(y%400==0||(y%100!=0&&y%4==0))

        {

     if(d>29||d<1)

     {

       return 0;

     }

        }

        else

        {

     if(d>28||d<1)

     {

       return 0;

     }

        }

        break;

   case 3:

        if(d>31||d<1)

        {

     return 0;

        }

        break;

   case 4:

        if(d>30||d<1)

        {

     return 0;

        }

        break;

   case 5:

        if(d>31||d<1)

        {

     return 0;

        }

   case 6:

        if(d>30||d<1)

        {

     return 0;

        }

        break;

   case 7:

        if(d>31||d<1)

        {

     return 0;

        }

        break;

   case 8:

        if(d>31||d<1)

        {

     return 0;

        }

        break;

   case 9:

        if(d>30||d<1)

        {

     return 0;

        }

        break;

   case 10:

        if(d>31||d<1)

        {

     return 0;

        }

        break;

   case 11:

        if(d>30||d<1)

        {

     return 0;

        }

        break;

   case 12:

        if(d>31||d<1)

        {

     return 0;

        }

        break;



 }

 if(h>24||h<0||mi>60||mi<0||s>60||s<0)

 {

   return 0;

 }

 return 1;

}

int year(char *x)

{

 char s[5];

 s[0]=x[6];

 s[1]=x[7];

 s[2]=x[8];

 s[3]=x[9];

 s[4]='\0';

 if(validDate(x))

 {

  return atoi(s);

 }

 else

 {

   return -1;

 }

}

int month(char *x)

{

 char s[3];

 s[0]=x[3];

 s[1]=x[4];

 s[3]='\0';

 if(validDate(x))

 {

   return atoi(s);

 }

 else

 {

   return -1;

 }

}

int date(char *x)

{

 char s[3];

 s[0]=x[0];

 s[1]=x[1];

 s[2]='\0';

 if(validDate(x))

 {

   return atoi(s);

 }

 else

 {

   return -1;

 }

}





int number_of_days_in_the_year(int x)

{

 if(x%400==0||(x%100!=0&&x%4==0))

 {

   return 366;

 }

 else

 {

   return 365;

 }

}

int number_of_days_till_now(char *x)

{

 int d,m,y,days=0;

 char s[3],s1[5];

 s[0]=x[0];

 s[1]=x[1];

 s[2]='\0';

 d=atoi(s);

 s[0]=x[3];

 s[1]=x[4];

 s[3]='\0';

 m=atoi(s);

 s1[0]=x[6];

 s1[1]=x[7];

 s1[2]=x[8];

 s1[3]=x[9];

 s1[4]='\0';

 y=atoi(s1);

 switch(m-1)

 {

   case 11:

         days+=30;

   case 10:

         days+=31;

   case 9:

         days+=30;

   case 8:

         days+=31;

   case 7:

         days+=31;

   case 6:

         days+=30;

   case 5:

         days+=31;

   case 4:

         days+=30;

   case 3:

         days+=31;

   case 2:

         if(y%400==0||(y%100!=0&&y%4==0))

         {

         days+=29;

         }

         else

         {

         days+=28;

         }

   case 1:

         days+=31;

   case 0:

         break;

 }

 days+=d;

 return days;

}




Bragaadeesh wrote at 2010-02-06 15:36:17
Hi, I have written a comprehensive program to give the difference between two dates in C. Please find the original work here.

http://technicalypto.blogspot.com/2010/02/different-between-two-days-implemented


Bragaadeesh wrote at 2010-02-12 21:51:37
The above URL is changed to this:

http://www.technicalypto.com/2010/02/different-between-two-days-implemented.html


LG915 wrote at 2013-01-07 02:57:50
http://ideone.com/3vuvYx is in simple C++ and works fine


C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Kishan

Expertise

I can answer questions in C related to programming, files pointers & data structures.

Experience

1+ year

Awards and Honors
Advanced System Software Certified

©2016 About.com. All rights reserved.