Solved

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

Posted on 2003-11-10
16
347 Views
Last Modified: 2010-04-01
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 :)
0
Comment
Question by:marcypark
  • 7
  • 3
  • 2
  • +4
16 Comments
 
LVL 4

Expert Comment

by:YuriPutivsky
ID: 9719427
// 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
0
 

Author Comment

by:marcypark
ID: 9719439
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.
0
 
LVL 4

Expert Comment

by:YuriPutivsky
ID: 9719454
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:marcypark
ID: 9719479
I am not sure I follow your code.  I'm sorry, I'm a newbie to this.
0
 
LVL 4

Expert Comment

by:YuriPutivsky
ID: 9719519
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. ....
0
 

Author Comment

by:marcypark
ID: 9719535
I am using VB 6.0 and I think I should maybe use a pointer?
0
 
LVL 30

Expert Comment

by:Axter
ID: 9719605
>>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...)
0
 

Author Comment

by:marcypark
ID: 9719611
yes sorry VC++
0
 
LVL 30

Expert Comment

by:Axter
ID: 9719723
Is your project an MFC program or Win32?
0
 

Author Comment

by:marcypark
ID: 9719775
win32 console application
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9720868
Does the professor require that you use pointers in this homework?
0
 

Author Comment

by:marcypark
ID: 9721810
No, I'm sorry, I am suppose to use strings.  And it's not a homework assignment, it's a non-graded self-exercise.
0
 

Author Comment

by:marcypark
ID: 9725596
Can someone help me?
0
 
LVL 4

Expert Comment

by:void_main
ID: 9866270
...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
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10248867
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
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10308083
PAQed - no points refunded (of 500)

modulo
Community Support Moderator
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Starting a function in a c++ application with a TCP command from C# 4 81
Socket Programming (Unix) 8 142
Exception thrown at 0x00007FFD5BC81F28 7 50
How do i run a c++ file? 15 47
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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