C语言基础:时间转换成字符串 strftime
#include <stdio.h> #include <time.h> int main(void) { char buffer[128]; struct tm *datetime; time_t current_time; tzset(); time(¤t_time); datetime = localtime(¤t_time); strftime(buffer, sizeof(buffer), "%x %X", datetime); printf("Using %%x %%X: %s\n", buffer); strftime(buffer, sizeof(buffer), "%A %B %m, %Y", datetime); printf("Using %%A %%B %%m %%Y: %s\n", buffer); strftime(buffer, sizeof(buffer), "%I:%M%p", datetime); printf("Using %%I:%%M%%p: %s\n", buffer); return 1; }
输出结果
Using %x %X: 09/09/12 18:10:33 Using %A %B %m %Y: Sunday September 09, 2012 Using %I:%M%p: 06:10PM