← Back to C Course | Chapter 14: Additional Topics | Lesson 7 of 9

C Date & Time

C can ask the computer for the current date and time and turn it into readable text, like a calendar and clock combined. You can also break it into day, month and year.
Syntax
c
#include <time.h>

time_t now = time(NULL);   // seconds since the Unix epoch
printf("%s", ctime(&now));   // human-readable text
struct tm *info = localtime(&now);

The time.h Header

The time.h header supplies the core types and functions C uses to work with calendar dates and times, most notably the time_t type for representing a single point in time and the struct tm type for representing that same point broken down into individual fields like year, month, and day.

Example: The time.h Header

c
// 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;
	struct tm *info;
	// Print formatted text to the screen
	printf("time.h provides time_t and struct tm");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Getting Current Time

time() returns the current calendar time as a time_t value, measured as the number of seconds that have elapsed since the Unix epoch (midnight, January 1, 1970 UTC) — a single portable number that other time.h functions can then convert into more human-friendly forms.

Example: Getting Current Time

c
// 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;
}

Formatting with ctime()

ctime() converts a raw time_t value into a ready-made, human-readable string containing the day of the week, month, date, time of day, and year, sparing you from manually assembling those pieces yourself, though its exact output format is fixed and not customizable.

Example: Formatting with ctime()

c
// 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("%s", ctime(&now));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Deconstructing Dates with localtime()

localtime() takes a time_t value and expands it into a struct tm with separate fields for the year, month, day, hour, minute, and second (all adjusted for the local time zone), which is what you need whenever you want to inspect or work with just one piece of a date rather than the whole thing at once.

Example: Deconstructing Dates with localtime()

c
// 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;
}

Custom Formats with strftime()

strftime() builds a custom-formatted date string using format specifiers similar to printf, such as %Y for the four-digit year or %B for the full month name, giving you complete control over a date's displayed format instead of being limited to ctime()'s single fixed layout.

Example: Custom Formats with strftime()

c
// 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[30];
	strftime(buffer, 30, "%Y-%B", local);
	// Print formatted text to the screen
	printf("%s", buffer);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting #include <time.h> before calling time or localtime.
  2. Storing the pointer from localtime and reusing it, since it points to a static buffer.
  3. Forgetting that tm_year counts from 1900 and tm_mon from 0.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.