Solved

today's date minus 7 days

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

820 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