我正在尝试从像"01/01/01"这样的字符串打印一个日期,并得到类似"2001年1月1日星期一"的内容.
我发现了一些与ctime的人有关但实际上并不知道如何使用它.
有帮助吗?
谢谢,
您可以使用strptime将字符串日期转换为struct tm
struct tm tm;
strptime("01/26/12", "%m/%d/%y", &tm);
Run Code Online (Sandbox Code Playgroud)
然后struct tm以适当的日期格式打印strftime
char str_date[256];
strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm);
printf("%s\n", str_date);
Run Code Online (Sandbox Code Playgroud)
strftime()做这个工作。
char buffer[256] = "";
{
struct tm t = <intialiser here>;
strftime(buffer, 256, "%H/%M/%S", &t);
}
printf("%s\n", buffer);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10053 次 |
| 最近记录: |