How do I printf a date in C?

14,180

Solution 1

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

Solution 2

strftime() does the job.

char buffer[256] = "";
{
  struct tm t = <intialiser here>;
  strftime(buffer, 256, "%H/%M/%S", &t);
}
printf("%s\n", buffer);

Solution 3

You're probably looking for strftime

Share:
14,180
Difender
Author by

Difender

Updated on June 26, 2022

Comments

  • Difender
    Difender almost 2 years

    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,