Link to home
Start Free TrialLog in
Avatar of DanPerlman
DanPerlman

asked on

DateAdd Equivalent in C

What is the best way to subtract 1 day from today's date in C?
Avatar of Dimkov
Dimkov

can you tell me what structure you are using for representation of the date?
Avatar of DanPerlman

ASKER

Maybe this helps some,

I am reading a file and need to find yesterdays date with a line. So I am trying to use the time function in c to give me yesterdays date to  compare in the file. I have something like this to get the current date into the format the date is in the file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

struct tm *ptr;
time_t lt;

int main(int argc, char** argv) {

    t = time(NULL);

    ptr = localtime(&lt);

    strftime(dt, 75, "%Y/%m/%d", ptr);

   printf(Date: %s \n, dt);

   return (EXIT_SUCCESS);
}

This will give we the date obviously, just not sure how to subtract a day from the calendar to accommodate for month and year.

working with tm is a bit hard, since it is a struct.
I would recomend you to work with time_t which is long. Here the date is represented in seconds passed from (00:00:00), January 1, 1970.
So if you subtrctract 60sec*60min*24h==1day=86400sec from the lt, you will get the previous day

t=t-86400;
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial