C语言标准宏__DATE__格式化YY/MM/DD
时间: 2023-08-30 19:05:30 浏览: 124
C语言标准宏__DATE__的格式为 "Mmm dd yyyy",其中Mmm为英文月份缩写,dd为日,yyyy为年份。要将其格式化为YY/MM/DD,可以使用以下方法:
```c
#include <stdio.h>
#include <string.h>
#define DATE_LEN 11
int main() {
char date[DATE_LEN];
char year[3], month[3], day[3];
// 将__DATE__复制到date数组中
strncpy(date, __DATE__, DATE_LEN);
// 将Mmm替换为数字月份
if (strncmp(date, "Jan", 3) == 0) {
strncpy(month, "01", 2);
} else if (strncmp(date, "Feb", 3) == 0) {
strncpy(month, "02", 2);
} else if (strncmp(date, "Mar", 3) == 0) {
strncpy(month, "03", 2);
} else if (strncmp(date, "Apr", 3) == 0) {
strncpy(month, "04", 2);
} else if (strncmp(date, "May", 3) == 0) {
strncpy(month, "05", 2);
} else if (strncmp(date, "Jun", 3) == 0) {
strncpy(month, "06", 2);
} else if (strncmp(date, "Jul", 3) == 0) {
strncpy(month, "07", 2);
} else if (strncmp(date, "Aug", 3) == 0) {
strncpy(month, "08", 2);
} else if (strncmp(date, "Sep", 3) == 0) {
strncpy(month, "09", 2);
} else if (strncmp(date, "Oct", 3) == 0) {
strncpy(month, "10", 2);
} else if (strncmp(date, "Nov", 3) == 0) {
strncpy(month, "11", 2);
} else if (strncmp(date, "Dec", 3) == 0) {
strncpy(month, "12", 2);
}
// 将dd和yyyy分别复制到day和year数组中
strncpy(day, date + 4, 2);
strncpy(year, date + 7, 2);
// 输出YY/MM/DD格式的日期
printf("%s/%s/%s\n", year, month, day);
return 0;
}
```
阅读全文