在c中获取日期和时间

dru*_*key 1 c string time date

我需要将日期作为一个字符串和当前时间作为另一个在C中.我已经看过time.h并看到它允许我得到整个事情.那我是否必须使用一些字符串方法?我注意到C没有子串方法?Strcpy看起来似乎可以工作,但我需要一个char*而不是一个数组.

有什么建议?

谢谢

cni*_*tar 6

你可以使用strftime这个:

struct tm *tm;
time_t t;
char str_time[100];
char str_date[100];

t = time(NULL);
tm = localtime(&t);

strftime(str_time, sizeof(str_time), "%H %M %S", tm);
strftime(str_date, sizeof(str_date), "%d %m %Y", tm);
Run Code Online (Sandbox Code Playgroud)