Hi,
I need to find the date 3 days ago from the current date (current date - 3) in C. Could anyone tell me how I can acheive this. Any help would be greatly appreciated.
Thanks
Printable View
Hi,
I need to find the date 3 days ago from the current date (current date - 3) in C. Could anyone tell me how I can acheive this. Any help would be greatly appreciated.
Thanks
Something along those lines should help.Code:#include <stdio.h>
#include <time.h>
time_t tt = time(NULL); // Seconds since midnight (00:00:00), January 1, 1970
time_t tt_3daysago = tt - 3 * 24 * 60 * 60; // minus 3 days in seconds
struct tm *t3daysago = localtime(&tt_3daysago);
printf("%s", asctime(t3daysago)
Read the current time in seconds and reduce 30*30*24*3=259200 from it. Use #include <time.h>
You can make all sorts of changes to tm_now, and the mktime() call will fix things up for you (within reason)Code:#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ( ) {
time_t now;
struct tm tm_now;
now = time(NULL);
printf( "Now=%s\n", asctime(localtime(&now)) );
tm_now = *localtime(&now);
tm_now.tm_mday -= 3; /* 3 days ago */
now = mktime( &tm_now );
printf( "Earlier=%s\n", asctime(localtime(&now)) );
return 0;
}