?
Solved

Date conversion in C

Posted on 2005-03-15
10
Medium Priority
?
216 Views
Last Modified: 2010-04-15
Dear Experts
I would like to have a code which would convert a dateformat from yyyymmdd to mmddyyyy
i.e 20050315 to 03152005

Regards
Ronan
0
Comment
Question by:ronan_40060
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13546189
Hi ronan_40060,

Here's some example code:

char OldTimeFormat[] = "20050315";
char NewTimeFormat[32];

strcpy(NewTimeFormat, OldTimeFormat+4);
strncat(NewTimeFormat, OldTimeFormat, 4);


That's it.  NewTimeFormat variable should have the new format.

You'll of course want to add test in above code for length of the source string.

David Maisonave :-)
Cheers!
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546424
Hello Axter
sorry  I did not get what you meant by to add test in above code for length of the source string

pls can u explain that
thank you very much
Ronan
0
 
LVL 30

Expert Comment

by:Axter
ID: 13546458
You want to add extra logic to make sure the source string is the minimum length.
Example:
if (strlen(OldTimeFormat) > 4)
{
   strcpy(NewTimeFormat, OldTimeFormat+4);
   strncat(NewTimeFormat, OldTimeFormat, 4);
}
else
{
   //Report error, or handle error here!!!
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Accepted Solution

by:
Axter earned 1000 total points
ID: 13546498
If OldTimeFormat is an empty string, or if it's smaller then 5 characters, then copying pass it's length will result in undefined behavior.

If your source string is a pointer, then you'll also want to make sure the pointer is not NULL.
Example:

bool ConvertTime(const char*OldTimeFormat, char*NewTimeFormat)
{
     if (OldTimeFormat && strlen(OldTimeFormat) > 4) //Could also add NULL test for NewTimeFormat
     {
        strcpy(NewTimeFormat, OldTimeFormat+4);
        strncat(NewTimeFormat, OldTimeFormat, 4);
     }
     else
     {
        //Report error, or handle error here!!!
        return false;
     }
     return true;
}
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546595
Axter here is the code . just one help
in printf statement what has to be entered ?


int main ()

{

char OldTimeFormat[] = "20050315";
char NewTimeFormat[32];

//strcpy(NewTimeFormat, OldTimeFormat+4);
//strncat(NewTimeFormat, OldTimeFormat, 4);

if (strlen(OldTimeFormat) > 4)
{
   strcpy(NewTimeFormat, OldTimeFormat+4);
   strncat(NewTimeFormat, OldTimeFormat, 4);
   printf("NewTimeFormat\n");      
}
else
{
   printf("Error reported !!!");
}

}
0
 
LVL 30

Expert Comment

by:Axter
ID: 13546627
Example:

printf("%s\n", NewTimeFormat);    
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546888
Axter Thanks a lot
regards
Ronan
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13546936
Axter a bit explanation needed
char NewTimeFormat[32];
why 32
and also
in the following
if (strlen(OldTimeFormat) > 4)
{
   strcpy(NewTimeFormat, OldTimeFormat+4);
   strncat(NewTimeFormat, OldTimeFormat, 4);
   printf("NewTimeFormat\n");    

why used 4
thank you
Ronan
0
 
LVL 30

Expert Comment

by:Axter
ID: 13546981
>>why 32

Just a common number I used.
You should use the largest size you expect your buffer to be, plus 1.
In your case that should be 9
char NewTimeFormat[9];

I myself always like to throw extra padding for good measure.

>>why used 4
Because you're copying after the forth position in your source string.

01234567  (zero based index into your string)
20050315
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 13547193
Great Axter
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

777 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