• C

How to convert a date in another format ?

Hi everybody,

just wanted your thoughts on the best way to convert a date from a format to another.

I currently have a date represented in m/d/yyyy and I need to convert it via a function to a yyyy/mm/dd format.

Thanks for your suggestions.
Frank
The_Kingpin08Asked:
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.

Kent OlsenData Warehouse Architect / DBACommented:
Hi The_Kingpin08,

There are countless ways to do this.  If you're that the input format is mm/dd/yy, then the easist is to use strtok() to split the strings.

If the format is m/d/yyyy, strtok() will do fine with a bit of help from atoi().


char OriginalDate[20] = "12/25/2004";
char *TempDate;
char NewDate[20];

char *Month, *Day, *Year;

  TempDate = strdup (OriginalDate);  /*  Make a temporary string that we can scribble into  */
  Month = strtok (TempDate, "/");
  Day = strtok (NULL, "/");
  Year = strtok (NULL, "");

  sprintf (NewDate, "%s/%s/%s", Year, Month, Day);


//if Month and/or Day can be a single digit, use this statement:

  sprintf (NewDate, "%s/%02d/%02d", Year, atoi(Month), atoi(Day));


Good Luck!
Kent
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
The_Kingpin08Author Commented:
Thanks a lot Kent.

Just 1 thinng, what does the printf does ?

Thanks
0
Kent OlsenData Warehouse Architect / DBACommented:
The_Kingpin08,

There are several functions in the printf() family.  I used sprintf() to generate a string in a particular format.  Related functions like printf(), fprintf(), vfprintf(), etc all do basically the same thing, except that they take slightly different inputs and write to a string (sprintf), a file (fprintf), or the terminal (printf).


Kent
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.