Reputation: 99
This code is for getting the current date and dislpaying it
#include stdio.h
#include time.h
int main(void)
{
char currentt[80];
time_t now = time(NULL);
struct tm *t = localtime(&now);
strftime (currentt,80,"%d/%m/%Y",t+=30);
puts (currentt);
printf("%s",currentt);
return 0;
}
And I have another code which adds 30 days to manually entered date
#include stdio.h
#include time.h
int main()
{
/* initialize */
int y=2014, m=9, d=19;
struct tm t = { .tm_year=y-1900, .tm_mon=m-1, .tm_mday=d };
/* modify */
t.tm_mday += 30;
mktime(&t);
/* show result */
printf(asctime(&t));
return 0;
}
What I want to do is merge this code in such a way that it gets the current date from FIRST code and ADD 30 days using the SECOND CODE.... can anyone help me with this. Any other logic will also be appreciated but i want it in C Language.
Upvotes: 0
Views: 76
Reputation: 47573
First #include
should be used with <
and >
around the file name. The code below is similar to the two above. I have put comments where appropriate. It simply gets the current time adds 30 days to the day field recomputes a new time and outputs it
#include <stdio.h>
#include <time.h>
int main()
{
/* Get the current time*/
time_t now = time(NULL);
struct tm *t = localtime(&now);
/* modify current time by adding 30 days*/
t->tm_mday += 30;
mktime(t);
/* show result */
printf(asctime(t));
return 0;
}
Upvotes: 1