Thread: current date - 3 days ago

  1. #1
    Unregistered
    Guest

    current date - 3 days ago

    Hi,

    I need to find the date 3 days ago from the current date (current date - 3) in C. Could anyone tell me how I can acheive this. Any help would be greatly appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66
    Code:
    #include <stdio.h>
    #include <time.h>
    
    time_t tt = time(NULL);   // Seconds since  midnight (00:00:00), January 1, 1970
    
    time_t tt_3daysago = tt - 3 * 24 * 60 * 60;  // minus 3 days in seconds
    
    struct tm *t3daysago = localtime(&tt_3daysago);
    
    printf("%s", asctime(t3daysago)
    Something along those lines should help.

  3. #3
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Read the current time in seconds and reduce 30*30*24*3=259200 from it. Use #include <time.h>

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ( ) {
        time_t  now;
        struct tm tm_now;
    
        now = time(NULL);
        printf( "Now=%s\n", asctime(localtime(&now)) );
        tm_now = *localtime(&now);
        tm_now.tm_mday -= 3;        /* 3 days ago */
        now = mktime( &tm_now );
        printf( "Earlier=%s\n", asctime(localtime(&now)) );
        return 0;
    }
    You can make all sorts of changes to tm_now, and the mktime() call will fix things up for you (within reason)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. Help Me Out!! Pls
    By Joanna in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 05:08 AM
  5. ctime - current date
    By razrektah in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2001, 12:42 PM