在C語言中,可以使用time.h頭文件中的函數來獲取當前系統時間。具體步驟如下:
#include <time.h>
time_t now;
struct tm *local_time;
time(&now);
local_time = localtime(&now);
int year = local_time->tm_year + 1900;
int month = local_time->tm_mon + 1;
int day = local_time->tm_mday;
int hour = local_time->tm_hour;
int minute = local_time->tm_min;
int second = local_time->tm_sec;
完整的代碼如下所示:
#include <stdio.h>
#include <time.h>
int main() {
time_t now;
struct tm *local_time;
time(&now);
local_time = localtime(&now);
int year = local_time->tm_year + 1900;
int month = local_time->tm_mon + 1;
int day = local_time->tm_mday;
int hour = local_time->tm_hour;
int minute = local_time->tm_min;
int second = local_time->tm_sec;
printf("當前時間:%d年%d月%d日 %d:%d:%d\n", year, month, day, hour, minute, second);
return 0;
}
運行該程序,即可獲取當前系統時間并打印出來。