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

ome*_*ega 3 c unix

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

我试过了

time_t now;
time(&now);
printf("%s", ctime(&now));
Run Code Online (Sandbox Code Playgroud)

但它给它像Fri Jan 11 23:50:33 2013......

谢谢

Mas*_*Man 5

你可以使用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);
}
Run Code Online (Sandbox Code Playgroud)

产量

Now it's 2013-01-12 10:33:13.
Run Code Online (Sandbox Code Playgroud)