问题
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