I have the below program to obtain current date and time.
int main(void)
{
time_t result;
result = time(NULL);
struct tm* brokentime = localtime(&result);
printf("%s", asctime(brokentime));
return(0);
}
And the output of the program is as follows :
Tue Aug 24 01:02:41 2010
How do I retrieve only the hour value say 01 from the above ?
Or is there any other system call where the currect hour of the system can be obtained ? I need to take an action based on this.
Thanks
If you want it as a number (and not a string), then just access the appropriate field in the
brokentimestructure:If you want it as a string, then you will have to format the string yourself (rather than using
asctime):Use
%Iinstead of%Hto get 12-hour time instead of 24-hour time.