In this tutorial, we will write a C program that prints the current date and time on console. First we are writing a basic program that fetches the local time from system and prints it on the screen. The explanation of this program is at the end of the code.
Later in this article, we have covered another program, where we learn how to print date and time in different formats.
#include <stdio.h>
#include <time.h>
int main() {
// Get current time in seconds since epoch
time_t t = time(NULL);
// Convert to local time and store in struct tm
struct tm *currentTime = localtime(&t);
// Formatting and printing the current date and time
printf("Current Date and Time: %02d/%02d/%04d %02d:%02d:%02d\n",
currentTime->tm_mday, currentTime->tm_mon + 1, currentTime->tm_year + 1900,
currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec);
return 0;
}
Output:
Current Date and Time: 14/05/2024 09:30:15
Explanation of the Program
Include Headers:
#include <stdio.h>
#include <time.h>
These lines include the necessary header files. <stdio.h>
is included for standard input-output operations such as printf
, and <time.h>
is included for time-related functions such as localtime
and time
.
Main Function:
int main()
This is the main function where the program execution begins.
Get Current Time:
time_t t = time(NULL);
This line declares a variable t
of type time_t
and assigns it the current time in seconds since the Unix epoch (00:00:00 UTC on 1 January 1970). The time()
function from the <time.h>
header is used for this purpose.
Convert to Local Time:
struct tm *currentTime = localtime(&t);
This line converts the current time (t
) to the local time representation and stores it in a struct tm
object named currentTime
. The localtime()
function from the <time.h>
header is used for this conversion.
Format and Print Current Date and Time:
printf("Current Date and Time: %02d/%02d/%04d %02d:%02d:%02d\n",
currentTime->tm_mday, currentTime->tm_mon + 1, currentTime->tm_year + 1900,
currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec);
This line prints the current date and time in a human-readable format. The format specifier %02d
ensures that each component (day, month, year, hour, minute, second) is printed with leading zeros if necessary. The values are extracted from the currentTime
structure using its members (tm_mday
, tm_mon
, tm_year
, tm_hour
, tm_min
, tm_sec
). Note that tm_mon
represents the month (0-11), so we add 1 to it to get the actual month.
Return Statement:
return 0;
This statement indicates that the program has executed successfully and returns 0 to the operating system.
C Program to Print Date and Time in different formats
This is an extension to the above program, here we will see how to print the current date and time in different formats.
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *currentTime = localtime(&t);
// Format 1: YYYY-MM-DD HH:MM:SS
printf("Format 1: %04d-%02d-%02d %02d:%02d:%02d\n",
currentTime->tm_year + 1900, currentTime->tm_mon + 1, currentTime->tm_mday,
currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec);
// Format 2: DD/MM/YYYY HH:MM:SS
printf("Format 2: %02d/%02d/%04d %02d:%02d:%02d\n",
currentTime->tm_mday, currentTime->tm_mon + 1, currentTime->tm_year + 1900,
currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec);
// Format 3: Month DD, YYYY HH:MM:SS
char months[12][10] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
printf("Format 3: %s %02d, %04d %02d:%02d:%02d\n",
months[currentTime->tm_mon], currentTime->tm_mday, currentTime->tm_year + 1900,
currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec);
return 0;
}
Output:
Format 1: 2024-05-14 10:45:30
Format 2: 14/05/2024 10:45:30
Format 3: May 14, 2024 10:45:30
Leave a Reply