Solved

cstring manipulation - east ponits

Posted on 2006-05-11
Medium Priority
226 Views
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
0
Question by:meatdog8
• 2

LVL 8

Expert Comment

ID: 16662605
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)
}
0

LVL 8

Expert Comment

ID: 16662629
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";
YMDTOMDY(Convert,'/');

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

LVL 14

Accepted Solution

wayside earned 200 total points
ID: 16711420
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);

0

LVL 1

Author Comment

ID: 16733895
wayside, thank you... worked great.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll