Solved

today's date minus 7 days

Posted on 2006-11-13
2
2,042 Views
Last Modified: 2008-02-01
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
Comment
Question by:arichexe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17936314
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
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 17936326
#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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 155
C++ :Change value from  DisableCMD registry 4 77
Resolve Dependency Issues 4 96
Autosar OS Multicore Share Resources confusion ? 2 185
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question