🕐⚡Beginner
Display Current Date and Time
Show system date/time
Display current date and time using the <time.h> library.
Program Code
current_datetime.c
C
1#include <stdio.h>2#include <time.h>34int main() {5 time_t now;6 struct tm *local;7 8 // Get current time9 time(&now);10 local = localtime(&now);11 12 printf("Current Date and Time:\n");13 printf("=======================\n");14 15 // Using ctime (simple format)16 printf("Simple: %s", ctime(&now));17 18 // Using struct tm (detailed)19 printf("\nDetailed:\n");20 printf("Date: %02d/%02d/%d\n", 21 local->tm_mday, 22 local->tm_mon + 1, // Month is 0-1123 local->tm_year + 1900); // Year since 190024 25 printf("Time: %02d:%02d:%02d\n",26 local->tm_hour,27 local->tm_min,28 local->tm_sec);29 30 printf("Day of week: %d (0=Sunday)\n", local->tm_wday);31 printf("Day of year: %d\n", local->tm_yday);32 33 return 0;34}Output
Current Date and Time:
=======================
Simple: Mon Dec 16 10:30:45 2024
Detailed:
Date: 16/12/2024
Time: 10:30:45
Day of week: 1 (0=Sunday)
Day of year: 350
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials