Thread: time.h making me wad grrrr

  1. #1
    qwerty
    Guest

    time.h making me wad grrrr

    well i have read up on the header file and i cant seem to find any function or any way to conver the date from that jumble of number(345345345345), simple text( sun janauary....) or an asm (690x00x6753) looking date to this format yyyymmdd! help this is driving me insane... i have tried everything i can think of in my compiler! im stumped

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Pass the address of the value you get from time() to the ctime() function. This is an ANSI function so should be implemented. There are probably a whole bunch of other routines on your system, but you don't say what that is, or what compiler you are using.

    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main( void )
    {
       time_t Time;
    
       time( &Time );
       printf( "The time is %s\n", ctime( &Time ) );
       return 0;
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Back in the days of Cobol this was a huge problem!

    Code:
    WORKING-STORAGE SECTION.
    01  DATE
         05   YEAR-OUT          PIC 4(X).
         05   MONTH-OUT      PIC 2(X).
         05   DAY-OUT           PIC 2(X).
    
    PROCEDURE-DIVISION.
    
    MOVE YEAR-IN TO YEAR-OUT
    MOVE MONTH-IN TO MONTH-OUT
    MOVE DAY-IN TO DAY-OUT.

  4. #4
    qwerty
    Guest
    im using msvc++ 4.0 and the ctime isnt quite what i want i tried that already i want the date to be stored like this yyyymmdd eg: 20020122 <- that would be todays date...

  5. #5
    Unregistered
    Guest
    pass the address of the time_t (or a time_t pointer) to localtime() and extract the month, day, year from the tm struct that is returned. Look in your online help section or the time.h file for the tm struct declaration.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Another idea is the function strftime().
    Code:
    #include <time.h>
    #include <iostream.h>
    
    int main( void )
    {
       time_t Time;
       char str[10];
    
       time( &Time );
       strftime(str,10,"%Y%m%d",localtime(&Time));
       cout << "date:" << str << endl;
       return 0;
    }

  7. #7
    qwerty
    Guest
    thank you swoopy!!!!! w00t! i knew i was close!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 07:00 AM
  2. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 11:59 PM
  3. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 01:42 PM
  4. Replies: 2
    Last Post: 01-13-2003, 01:28 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM