C 语言中的 date
在 C 语言中,
date是 time.h 头文件中定义的结构体,用于表示日期和时间信息。
结构体定义:
<code class="c">struct tm {
int tm_sec; // 秒 (0-59)
int tm_min; // 分钟 (0-59)
int tm_hour; // 小时 (0-23)
int tm_mday; // 日 (1-31)
int tm_mon; // 月 (0-11,0 表示 1 月)
int tm_year; // 年(自 1900 年起)
int tm_wday; // 星期 (0-6,0 表示星期日)
int tm_yday; // 年内日 (0-365,0 表示 1 月 1 日)
int tm_isdst; // 夏令时标志 (0-1,0 表示无夏令时)
};</code>成员含义:
立即学习“C语言免费学习笔记(深入)”;
tm_sec: 秒,范围为 0-59。
tm_min: 分钟,范围为 0-59。
tm_hour: 小时,范围为 0-23。
tm_mday: 月份中的日期,范围为 1-31。
tm_mon: 月份,范围为 0-11,0 表示 1 月。
tm_year: 自 1900 年起以来的年数。
tm_wday: 星期,范围为 0-6,0 表示星期日。
tm_yday: 年内日,范围为 0-365,0 表示 1 月 1 日。
tm_isdst: 夏令时标志,0 表示无夏令时,1 表示有夏令时。
用法:
date结构体可以通过
time()函数获取当前日期和时间,并将其存储在
date结构体中。以下是使用
date的示例:
<code class="c">#include <stdio.h>
#include <time.h>
int main() {
struct tm *current_time;
time_t rawtime;
time(&rawtime);
current_time = localtime(&rawtime);
// 打印当前日期和时间
printf("当前日期和时间为:%s", asctime(current_time));
return 0;
}</code>输出:
<code>当前日期和时间为:Sat Jan 1 00:00:00 1900</code>
