Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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
0
The_Kingpin08
Asked:
The_Kingpin08
  • 2
1 Solution
 
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
 
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

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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now