Hi All,

I want a C programme which will take a user input as a date and number of days,

and then it will add the given number of days to the input date and will give the resulted date.


It should keep in mind all the permutation and combinations like changing month year , leap year, some months have 31 days some have 30 days etc....

for eg.:
input date 2010-12-25
days 20
result 2011-01-14

This programme can be made but this will take a lot of time if anybody has similar to that or exact programme then please help....
its just a module.

Thanks

Recommended Answers

All 13 Replies

After getting the date and number of days, use functions/structures in time.h to do all the hard work. First populate a struct tm with the year,month and day from keyboard input, then add in the number of days to mdays field of the structure. Finally call mktime() and it will normalize all the fields.

i wish to have a sample example to use the functions u said.
please help me

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

int main()
{
  int day = 1;
  imt month = 2; // February
  int year = 2010; // year can not be before 1900
  int days_to_add = 123;
  struct tm tm;
  // initialize the structure
  memset(&tm,0,sizeof(struct tm));
  tm.tm_year = year-19000; 
  tm.tm_mon = month-1; // 0 = Jan, 1 = Feb etc
  tm.tm_mday = day;
  tm.tm_hour = 12; // fictious
  tm.tm_mday += days_to_add;
  mktime(&tm);

}

i think you willingly did a mistake writing 19000
is it just 1900?

please can u explain the functions used in it....

i think you willingly did a mistake writing 19000
is it just 1900?

It was a typo. 1900 is correct.

please can u explain the functions used in it....

There is only one function -- learn to use google for stuff you do not understand.

k thanks
and can i know some other functions of such kind from you plss

If you want to create this function then try this one..

void  make_date(int *d,int *m,int *y,int days)
{
  int flag=0,temp=0;
  // variable days must have positive integer value
 
  while(flag <days)
  {
     (*d) ++;
     if( (*m) == 4 || (*m) == 6 || (*m) == 9 || (*m) == 11)
      {
           temp=30;
      }
     else if( (*m) == 2)
      {
           if( (*y) % 400 == 0 || ( (*y) % 100 != 0 && (*y) % 4 == 0 ))

               temp = 29;
           else
               temp = 28;
      }
     else
          temp=31;

     if( (*d) > temp)
      {
          (*m) ++;
          (*d) = 1;
      }
     if( (*m) == 13)
      {
          (*y) ++;
          (*m) = 1;
      }
     flag++;
 }  // end of while
  return;
} // end of function

Thanks Ancient Dragon for your reply


there are some who when asking them the example dont give example.....

but thanks a lot for ur reply .

Now the problem is solved and I can use the programme.


actually it was some what different

we have to enter the date and the hours to run some partivular programme.....
so we have to check forst for the feasibility of the entered hour then converting it to no of days and then adding the days and the remaining no of hours.....

but your code instead of converting it to days i directly added the no of hours to the current date time.

Yes, it is a little different than what I originally thought -- its actually a lot simpler. Convert hours to days just divide hours by 24. The remainder of that division will be the excess hours.

int hours = 123; // you enter this value
int days = hours/24;
hours = hours - (days * 24);

Yeah....

Your programme was the same....only some mathematical calculations with some validations were required ...

but your code helped a lot and Its completed now ...

Thanks again for ur help....

also i know that the converting was not the difficult task I just wrote to let you know the actual thing .... dont take it other way....

thanks sir i got ur program

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.