Solved

How to convert a date in another format ?

Posted on 2004-10-26
172 Views
Last Modified: 2010-04-15
Hi everybody,

just wanted your thoughts on the best way to convert a date from a format to another.

I currently have a date represented in m/d/yyyy and I need to convert it via a function to a yyyy/mm/dd format.

Thanks for your suggestions.
Frank
0
Question by:The_Kingpin08
    3 Comments
     
    LVL 45

    Accepted Solution

    by:
    Hi The_Kingpin08,

    There are countless ways to do this.  If you're that the input format is mm/dd/yy, then the easist is to use strtok() to split the strings.

    If the format is m/d/yyyy, strtok() will do fine with a bit of help from atoi().


    char OriginalDate[20] = "12/25/2004";
    char *TempDate;
    char NewDate[20];

    char *Month, *Day, *Year;

      TempDate = strdup (OriginalDate);  /*  Make a temporary string that we can scribble into  */
      Month = strtok (TempDate, "/");
      Day = strtok (NULL, "/");
      Year = strtok (NULL, "");

      sprintf (NewDate, "%s/%s/%s", Year, Month, Day);


    //if Month and/or Day can be a single digit, use this statement:

      sprintf (NewDate, "%s/%02d/%02d", Year, atoi(Month), atoi(Day));


    Good Luck!
    Kent
    0
     

    Author Comment

    by:The_Kingpin08
    Thanks a lot Kent.

    Just 1 thinng, what does the printf does ?

    Thanks
    0
     
    LVL 45

    Expert Comment

    by:Kdo
    The_Kingpin08,

    There are several functions in the printf() family.  I used sprintf() to generate a string in a particular format.  Related functions like printf(), fprintf(), vfprintf(), etc all do basically the same thing, except that they take slightly different inputs and write to a string (sprintf), a file (fprintf), or the terminal (printf).


    Kent
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    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 and use conditional statements in the C programming language.

    933 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

    16 Experts available now in Live!

    Get 1:1 Help Now