How can I calculate the current date and time in C lang…

In C language, you can use functions in the time.h header file to retrieve the current date and time. Below is an example code:

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

int main() {
    // 获取当前时间
    time_t currentTime = time(NULL);

    // 将时间转换为本地时间
    struct tm* localTime = localtime(&currentTime);

    // 获取年月日
    int year = localTime->tm_year + 1900;
    int month = localTime->tm_mon + 1;
    int day = localTime->tm_mday;

    // 输出年月日
    printf("现在的年月日时间是:%d年%d月%d日\n", year, month, day);
    
    return 0;
}

Running this code will display the current date and time.

bannerAds