How do I printf a date in C?

孤者浪人 提交于 2019-12-05 23:07:37

问题


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,


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



回答2:


strftime() does the job.

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



回答3:


You're probably looking for strftime



来源:https://stackoverflow.com/questions/24161818/how-do-i-printf-a-date-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!