Solved

today's date minus 7 days

Posted on 2006-11-13
2
2,020 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
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now