How can I convert a timestamp to a date format in the C programming language?

You can use the time and localtime functions in C language to convert a timestamp to a date format. Below is a simple example code:

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

int main() {
    time_t timestamp = 1597414471; // 2020-08-14 15:34:31 的时间戳
    struct tm *tm_info;

    tm_info = localtime(&timestamp);

    printf("Year: %d\n", tm_info->tm_year + 1900);
    printf("Month: %d\n", tm_info->tm_mon + 1);
    printf("Day: %d\n", tm_info->tm_mday);
    printf("Hour: %d\n", tm_info->tm_hour);
    printf("Minute: %d\n", tm_info->tm_min);
    printf("Second: %d\n", tm_info->tm_sec);

    return 0;
}

In this example, we start by defining a timestamp, then use the localtime function to convert the timestamp into a struct tm structure, which includes information such as year, month, day, hour, minute, and second. Finally, we access the members of the struct tm structure to obtain specific date information.

bannerAds