Solved

today's date minus 7 days

Posted on 2006-11-13
2
2,005 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
Comment Utility
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
Comment Utility
#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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now