如何从time_t/timeval获取iso_date(YYYYMMDD)

Hum*_*ger 4 c datetime boost unix-timestamp

给定time_tas 1291121400,如何将当天的日期格式化为20101130

nel*_*age 5

使用gmtime(3)localtime(3)将其转换为struct tm (或更好,可重入的版本gmtime_rlocaltime_r),然后用strftime(3)它将其转换为字符串.例如,如果您想要UTC格式的输出:

struct tm tm;
char buf[9];
gmtime_r(&my_time_t, &tm);
strftime(buf, sizeof(buf), "%Y%m%d", tm);
printf("The date is: %s\n", buf);
Run Code Online (Sandbox Code Playgroud)