[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


How to convert a date in another format ?

Posted on 2004-10-26
Medium Priority
Last Modified: 2010-04-15
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.
Question by:The_Kingpin08
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
  • 2
LVL 46

Accepted Solution

Kent Olsen earned 1000 total points
ID: 12417222
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!

Author Comment

ID: 12417253
Thanks a lot Kent.

Just 1 thinng, what does the printf does ?

LVL 46

Expert Comment

by:Kent Olsen
ID: 12417314

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


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

649 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