smmr
smmr

Reputation: 33

How to get time from date time

Is there a function like ctime that will get just the hours, minutes, and seconds as a char*?

I've tried

time_t time
struct tm* currtime = localtime(time);
printf("%d:%d:%d", currtime->tm_hour, currtime->tm_min, currtime->tm_sec);

but in the case of single digit values it'll print 8:2:5 instead of 08:02:05.

Upvotes: 0

Views: 75

Answers (1)

June
June

Reputation: 408

try the following:

`
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
char dateTime[10 + 1] = {0};

strftime(dateTime, 10, "%H%M%S", localtime(&cur_time.tv_sec));
`

Upvotes: 1

Related Questions