C time.h Functions
In this page:
#include <time.h>
time_t now = time(NULL);
ctime(&now); // readable date string
Getting Current Time
time() returns the current calendar time as the number of seconds elapsed since the Unix epoch (midnight, January 1, 1970 UTC), giving you a single portable integer value you can store, compare, or later convert into a readable date.
It's the usual starting point for any time-related calculation in C.
Example: Getting Current Time
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
// Print formatted text to the screen
printf("%d", now > 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Formatting Time as String
ctime() takes the raw time_t value returned by time() and formats it into a fixed, human-readable string containing the day of week, month, date, time, and year, saving you from manually assembling a date string yourself.
The returned string already ends in a newline, which is easy to forget when concatenating it elsewhere.
Example: Formatting Time as String
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
char *str = ctime(&now);
// Print formatted text to the screen
printf("%d", str != NULL);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Deconstructing Time
localtime() breaks a time_t value down into a struct tm with separate fields for year, month, day, hour, minute, and second, adjusted for the local time zone, so you can inspect or manipulate individual components of a date instead of working with the single opaque seconds-since-epoch number.
Example: Deconstructing Time
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
struct tm *local = localtime(&now);
// Print formatted text to the screen
printf("%d", local->tm_year + 1900);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Measuring Execution Time
clock() reports how many CPU clock ticks have elapsed since the program itself started running, which is useful for measuring how long a specific piece of code takes to execute.
Dividing the result by CLOCKS_PER_SEC converts that tick count into a number of seconds you can actually reason about.
Example: Measuring Execution Time
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
clock_t start = clock();
// Loop: repeat while the condition holds
for (int i = 0; i < 1000; i++) {}
clock_t end = clock();
// Print formatted text to the screen
printf("%d", end >= start);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Custom Time Formatting
strftime() lets you build a custom date string using format specifiers similar in spirit to printf's, such as %Y for a four-digit year or %H for a 24-hour-clock hour, giving you full control over exactly how a date is displayed instead of being limited to ctime()'s fixed format.
Example: Custom Time Formatting
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
struct tm *local = localtime(&now);
char buffer[20];
strftime(buffer, 20, "%Y", local);
// Print formatted text to the screen
printf("%s", buffer);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting
#include <time.h>before usingtimeorclock. - Storing the result of
ctimeand using it later, when it returns a static buffer overwritten by the next call. - Dividing the value of
clock()by the wrong constant when converting to seconds instead ofCLOCKS_PER_SEC.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions