C语言计算日期时间:根据秒数获取当前日期和时间
以下是用C语言实现计算当前日期和时间的代码:
#include <stdio.h>
#include <time.h>
int main()
{
time_t now = time(NULL);
struct tm *current_time = localtime(&now);
printf('Current date and time: %s', asctime(current_time));
return 0;
}
上面的代码中,我们使用了time()函数来获取当前的时间戳,然后使用localtime()函数将时间戳转换为struct tm结构体类型的本地时间。最后,我们使用asctime()函数将本地时间转换为字符串,打印出来。
原文地址: https://www.cveoy.top/t/topic/nuNn 著作权归作者所有。请勿转载和采集!