#include <stdio.h>
#include <time.h>
int main() {
time_t now = time(NULL); // 获取当前时间的时间戳
struct tm *loctm = localtime(&now); // 将时间戳转换为本地时间的tm结构
char buf[100]; // 定义一个缓冲区来存储格式化后的字符串
// 使用strftime()格式化时间
// %m - 月份(01-12)
// %d - 日期(01-31)
// %y - 年份的后两位数字(00-99)
// %H - 小时(00-23)
// %M - 分钟(00-59)
// %S - 秒(00-59)
strftime(buf, sizeof(buf), "%m/%d/%y %H:%M:%S", loctm);
// 打印格式化的时间
printf("格式化的时间: %s\n", buf);
return 0;
}
结果:格式化的时间: 05/20/24 15:45:30