How can I get the current time in the C language?

In C language, you can use functions from the time.h header file to obtain the current time.

You can use the time function to get the current system time. The time function returns the number of seconds since January 1, 1970 (timestamp). For example:

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

int main() {
    time_t now;
    time(&now);
    printf("当前时间的时间戳:%ld\n", now);
    
    return 0;
}

The output is similar to:

当前时间的时间戳:1621897336

To convert a timestamp to local time, you can use the localtime function. Here is an example:

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

int main() {
    time_t now;
    struct tm *t;
    time(&now);
    t = localtime(&now);
    printf("当前本地时间:%d-%d-%d %d:%d:%d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
    
    return 0;
}

The output will be similar to:

当前本地时间:2021-5-25 10:17:16
bannerAds