gpt4 book ai didi

c - 如何以 2013-01-11 23 :34:21? 格式获取日期和时间

转载 作者:太空狗 更新时间:2023-10-29 16:02:21 24 4
gpt4 key购买 nike

在我的 C 程序中,如何获取格式为 2013-01-11 23:34:21 的日期和时间?

我试过了

time_t now;
time(&now);
printf("%s", ctime(&now));

但它给出的是 Fri Jan 11 23:50:33 2013...

谢谢

最佳答案

您可以使用 strftime() 来自 <time.h> 的函数

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

int main() {
time_t now;
time(&now);

struct tm* now_tm;
now_tm = localtime(&now);

char out[80];
strftime (out, 80, "Now it's %Y-%m-%d %H:%M:%S.", now_tm);

puts(out);
}

输出

Now it's 2013-01-12 10:33:13.

关于c - 如何以 2013-01-11 23 :34:21? 格式获取日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290363/

24 4 0
文章推荐: html - 在 CSS 问题中为