Link to home
Start Free TrialLog in
Avatar of marcypark
marcypark

asked on

Program to convert format from 12/1/1999 to December 1, 1999

How can you convert a date in the format 07/21/1955 to something like July 21, 1955 by using pointers?  Help me out a little please :)
Avatar of YuriPutivsky
YuriPutivsky

// use COleDateTime class
COleDateTime convert;
// here we go...
if (!convert.ParseDateTime("07/21/1955", VAR_DATEVALUEONLY, LANG_USER_DEFAULT))
// error handler
    return;

// and back...
CString str = convert.Format("%B %d, %Y");
// str will looks like: July 21, 1955
Avatar of marcypark

ASKER

I need to use this format for any date entered; prompting the user to enter a date in the form mm/dd/yyyy and then printing it like month day, year.
So?
define a buffer - big enough
char buf[64];

// get user input here and populate buf
// then
// use COleDateTime class
COleDateTime convert;
// here we go...
if (!convert.ParseDateTime(buf, VAR_DATEVALUEONLY, LANG_USER_DEFAULT))
// error handler
    return;

// and back...
CString str = convert.Format("%B %d, %Y");
// here str contains what user printed in the new format
I am not sure I follow your code.  I'm sorry, I'm a newbie to this.
OK,
Your question was how to convert user input data in "mm/dd/yyyy" format to the new format
 "Month dd, yyyy".
The answer you get a char buffer from user (how? this is another question)
Then using COleDateTime class you convert user input into suitable format.
Now you are reay to display the entered date in new format (how? again it depends what do you want to use).

1. cout << buf;
2. printf("%s", buf);
3. ....
I am using VB 6.0 and I think I should maybe use a pointer?
Avatar of Axter
>>I am using VB 6.0 and I think I should maybe use a pointer?

Do you mean you're using VC++ 6.0????

This is the C++ topic area.  If you have a VB question, I recommend you post it in the Visual Basic topic area.

If you're coding in C++ please post what compiler you're using, and what type of project (MFC, Win32, UNIX, etc...)
yes sorry VC++
Is your project an MFC program or Win32?
win32 console application
Does the professor require that you use pointers in this homework?
No, I'm sorry, I am suppose to use strings.  And it's not a homework assignment, it's a non-graded self-exercise.
Can someone help me?
...yes:

you have a string for the input like this:
char *originalDate = "12/07/2003";

then you have to parse the string like this:
int values[3];  // 0 is month   1 is day   2 is year
int where = 0; // Which value is processed now? 0 = month ......
int oldX = 0;   // internal for token parsing
char before;   // last token
for (int x = 0; x < strlen(originalDate); x++)
{
        if ((originalDate[x] == '/') || (originalDate[x] == 0))      // found a   /     or a \0 (string termination sign)
        {
            before = orignalDate[x];        // What delimeter we HAD
            originalDate[x] = 0;               // set it to zero (temp.)

            values[where] = atoi(originalDate + oldX);     // atoi means string to integer

            oldX = x + 1;                        // Store the last position we were
            where++;                             // each time we find a new token, we should go to the next variable

            originalDate[x] = before;       // replace the old token
        }
}

then you have to reformat the string, using a table like this:
char *months[12] = {"January", "February", "March", "April", ....., "December"};

finally you need a new buffer to write your results:
char newBuffer[100];
sprintf(newBuffer, "%s %i, %i", months[values[0] - 1], values[1], values[2]);
// note: %s is replaced by the String in monts i.e. "April"
//          %i is replaced by the Integer in values[1] and [2]   in order they are listed

now your buffer "newBuffer" contains "December 12, 2003" you can printf like this:
printf("%s\n", newBuffer);

IMPORTANT note: this is pseudocode. You also have to include the correct header-files for strlen and atoi
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ - no points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial