getting date and time in c

佐手、 提交于 2020-01-14 04:15:16

问题


I need to get the date as one string and the current time as another in C. I've had a look at time.h and see it allows me to get the whole thing. Do I then have to use some string methods? I notice that C doesn't have a substring method? Strcpy seems like it might work but I need a char * not an array.

Any advice?

Thanks


回答1:


You can use strftime for this:

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);


来源:https://stackoverflow.com/questions/9233123/getting-date-and-time-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!