简体   繁体   中英

How do I printf a date in C?

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,

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

strftime() does the job.

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

你可能正在寻找strftime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM