cstring manipulation - east ponits

Posted on 2006-05-11
Last Modified: 2010-04-01
I have a CString varible with a date in "YYYYMMDD" format.  I need to make the date in this format "MMDDYY" any ideas? thanks in advance
Question by:meatdog8
    LVL 8

    Expert Comment

    A function like this could take a string and change the date.
    void YMDTOMDY(char *String,char Delim=0)
          bool UD = false; //Check if delimiter is used (when Delim is 0, it is not used)
          if (Delim !=0) UD = true;
          char Year[2];
          Year[0] = String[2]; //Store the year from the string
          Year[1] = String[3];

          String[0] = String[4+UD]; //Set the first bit to the month (+1 for delimiter if there)
          String[1] = String[5+UD];
          if (UD == true)
                String[2] = Delim; //If there is a delimiter, use one too
          String[2+UD] = String[6+(UD*2)];//set next bit of string to the day +2 for delimiter if there
          String[3+UD] = String[7+(UD*2)];
          if (UD == true)
                String[5] = Delim;//if delimiter, use one for ours too
          String[4+(UD*2)] = Year[0];//set last bit to our year which we saved
          String[5+(UD*2)] = Year[1];

          String[6+(UD*2)] = '\0'; //End string since we shortened it. (add terminator)
    LVL 8

    Expert Comment

    Then you could just make a call with your original string such as:

    char Convert[9]="20060511";
    YMDTOMDY(Convert); //no need for delimiter parameter due to default =0 assignment

    OR (With a delimiter)

    char Convert[11]="2006/05/11";

    And you will result in your Convert being MM(/)DD(/)YY (Where (/) is optional and cusomizable)
    LVL 14

    Accepted Solution

    No need to deal with char *'s when working with CString. Plus, if you pass in a static string like "20060518" to the above function it will most likely crash, because static strings are generally stored in a RO section.

    Try this:

    CString oldDate("20060518");

    // convert to MMDDYY
    CString newDate = oldDate.Mid(4,2) + oldDate.Mid(6,2) + oldDate.Mid(2,2);

    LVL 1

    Author Comment

    wayside, thank you... worked great.  

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    754 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

    20 Experts available now in Live!

    Get 1:1 Help Now