How do I printf a date in C?

Difender picture Difender · Jun 11, 2014 · Viewed 13.1k times · Source

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,

Answer

HAL picture HAL · Jun 11, 2014

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