Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date functions - date arithmatic in C? 1

Status
Not open for further replies.

Sudmill

Programmer
Apr 20, 2001
65
GB
Hi,

Im looking for a function that allows me to add or subtract days to a date, e.g. to return the date of 9 days ago.
Im sure that I have seen a date manipulation fn at some point, but I may just be going crazy ...(probably thinking about the dateadd MSSQL fn !)

Is the only way to do it manually (if so then how do I determine the last day of the previous month, i.e. 28/29/30/31st? and leap years etc ... ).

Or do I just subtract x days from the tm.tm_mday structure? (I doubt this is the solution)

Looking forward to hearing you ideas on this one.

Cheers

John (Sudmill)
 
There's a bunch of ways of doing this (e.g., mktime). This is a fairly straightforward method:

struct tm *ptm;
time_t t;

time(&t); // get current time
t -= 9 * 24 * 60 * 60; // subtract 9 days from now
ptm = localtime(&t); // convert to struct tm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top