Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2059
  • Last Modified:

today's date minus 7 days

How would I write a C function that returns today's date minus 7 days?  For instance, if today's date was 11/13/2006, it would return 11/6/2006.
0
arichexe
Asked:
arichexe
1 Solution
 
sunnycoderCommented:
Hi arichexe,

time() would give you current time in seconds since epoch ...
Subtract 7 * 24 * 3600 from this value ... this would give you time 7 days ago ...
To get other representations of this time, use ctime/ctime_r etc

time_t val = time (NULL);
val = val - (7 * 24 * 3600);
printf ("%s",ctime(&val));

Cheers!
sunnycoder
0
 
ozoCommented:
#include <stdio.h>
#include <time.h>
main(){
 time_t t = time(0);
 t -= 7*24*60*60;
 char buffer[13];
 strftime(buffer,sizeof(buffer),"%m/%d/%Y",localtime(&t));
 puts(buffer);
}


although there in most places, there are fourteen hours per year near a daylight savings time switchover when the above would be off by a day.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now