C Program: Display Date & Time

Here is a simple example code for displaying the current time and date in C language.

#include <stdio.h>
#include <time.h>

int main() {
    // 获取当前时间
    time_t rawtime;
    time(&rawtime);
    struct tm *timeinfo = localtime(&rawtime);

    // 显示日期
    printf("当前日期: %02d-%02d-%04d\n", timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_year + 1900);

    // 显示时间
    printf("当前时间: %02d:%02d:%02d\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

    return 0;
}

Please note that the above code uses the time() function to obtain the current time and then uses the localtime() function to convert the time to local time. Next, we can use the various members of the struct tm structure to obtain different parts of the date and time. Finally, the formatted date and time will be output to the users using the printf() function.

bannerAds