DateAdd Equivalent in C

Posted on 2007-08-09
Last Modified: 2013-11-05
What is the best way to subtract 1 day from today's date in C?
Question by:DanPerlman
    LVL 3

    Expert Comment

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

    Author Comment

    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.

    LVL 3

    Expert Comment

    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

    LVL 45

    Accepted Solution


    Converting dates is one of the most irritating things in C.  There are just too many little details that one works with so seldomly.

    #include <time.h>
    main ()
      time_t rawtime;
      struct tm * timeinfo;

      time ( &rawtime );                            // time resolution in seconds
      rawtime += (7 * 86400);                 // add a week to the current date
      timeinfo = localtime ( &rawtime );
      mktime ( timeinfo );

    /* All of the fields of  struct tm *timeinfo are now populated.  They can be formatted for display or used in other calculations.

    The fields are:

    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
      return 0;

    Good Luck,

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    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 opening and reading files in the C programming language.

    734 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

    20 Experts available now in Live!

    Get 1:1 Help Now