Thread: Problem with strftime

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Problem with strftime

    Im trying to do a console a program and i need to get the day, and month and, year into diferent int's, but i cant get any of them, im trying to at least get the month, but i cant
    this is what i do
    Code:
    int mes;
    time_t now;
    struct tm *tm_now;
    char buff[20];
    now = time ( NULL );
    tm_now = localtime ( &now );
    strftime(buff, sizeof buff, "%m", tm_now );
    mes=atoi(buff);
    note: mes = month in spanish
    everytime i try to use mes i get an error

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why not use the broken-down time resulting from the call to localtime?
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
       int mes;
       time_t now;
       struct tm *tm_now;
       now = time ( NULL );
       tm_now = localtime ( &now );
       if ( tm_now )
       {
          mes=tm_now->tm_mon + 1;
          printf("mes = %d\n", mes);
       }
       return 0;
    }
    
    /* my output
    mes = 3
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    Thanks, it works fine now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM