8

I'm trying to print a date from a string like "01/01/01" and get something like "Monday First January 2001.

I found something with the man of ctime but really don't get it how to use it.

Any help ?

Thanks,

Difender
  • 495
  • 2
  • 5
  • 18

3 Answers3

8

You can use strptime to convert your string date to struct tm

struct tm tm;
strptime("01/26/12", "%m/%d/%y", &tm);

And then print struct tm in the appropriate date format with strftime

char str_date[256];
strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm);
printf("%s\n", str_date);
HAL
  • 3,888
  • 3
  • 19
  • 28
5

strftime() does the job.

char buffer[256] = "";
{
  struct tm t = <intialiser here>;
  strftime(buffer, 256, "%H/%M/%S", &t);
}
printf("%s\n", buffer);
alk
  • 69,737
  • 10
  • 105
  • 255
  • The signature of `strftime` is `size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);`. You're missing the `size_t` parameter. – HAL Jun 11 '14 at 11:57
1

You're probably looking for strftime

Shan-x
  • 1,146
  • 6
  • 19
  • 44