Thread: Date conversion

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6

    Question Date conversion

    Hi guys,

    I know how to convert current date into whatever format,

    eg. 2006-01-10, or 10-01-06

    But, now I have a string that is "10.01.06" (DD.MM.YY), and how do you guys convert it to 2006-01-10 string using the time/date function under C?

    Any input would be appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6
    I can definitely use strtok to deal with it, but is there any quicker way by using time/date function under C?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Using time functions, I might do something like this.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
       struct tm local;
       char text[20] = "10.01.06";
       if ( sscanf(text, "%d.%d.%d", &local.tm_mday,
                   &local.tm_mon, &local.tm_year) == 3 )
       {
          local.tm_mon  -= 1;
          local.tm_year += 100;
          if ( strftime(text, sizeof text, "%Y-%m-%d", &local) )
          {
             puts(text);
          }
       }
       return 0;
    }
    
    /* my output
    2006-01-10
    */
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM