• C

Date conversion in C

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
LVL 9
ronan_40060Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterCommented:
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
ronan_40060Author Commented:
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
AxterCommented:
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
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

AxterCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ronan_40060Author Commented:
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
AxterCommented:
Example:

printf("%s\n", NewTimeFormat);    
0
ronan_40060Author Commented:
Axter Thanks a lot
regards
Ronan
0
ronan_40060Author Commented:
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
AxterCommented:
>>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
ronan_40060Author Commented:
Great Axter
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.