C program to print the current date-time in different formats

C program to print the current time, date, month and year:

This C program tutorial will show you how to print the current system time, date, month and year values in different formats. You will learn how to use the time.h header file and a few of its functions.

C program to print the current date-time in system default format:

The below program prints the date-time in system default format:

#include<stdio.h>
#include<time.h>
int main()
{
    time_t t;
	time(&t);

    printf("System date/time: %s", ctime(&t));
    return 0;
}

It will print the result as below:

System date/time: Sat Sep 17 13:05:53 2022

Here,

  • time_t is a data type to store the system time.
  • The time function assigns the system time to the *time_t variable t.
  • The ctime() function is defined in the time.h header file. It returns the string representation of the current system time.

How to access the hour, minute, second, date, month and year:

We can access the hour, minute, second, date, month and year values by using a function called localtime.

#include <stdio.h>
#include <time.h>
int main()
{
	time_t t;
	struct tm *time_data;

	time(&t);
	time_data = localtime(&t);

	printf("Day: %d\n", time_data->tm_mday);
	printf("Month: %d\n", time_data->tm_mon + 1);
	printf("Year: %d\n", time_data->tm_year + 1900);

	printf("Hour: %d\n", time_data->tm_hour);
	printf("Min: %d\n", time_data->tm_min);
	printf("Sec: %d\n", time_data->tm_sec);
	return 0;
}
  • The localtime function can be used to get the local system time.
  • We have to pass a time_t variable pointer to this function.
  • It returns a pointer to struct tm object, the time_data variable in this example.
  • This object can be used to get the other values, tm_mday to get the day, tm_mon to get the month, tm_year to get the year, tm_hour to get the hour, tm_min to get the minute and tm_sec to get the second.

If you run this program, it will print output as below:

Day: 17
Month: 9
Year: 2022
Hour: 13
Min: 16
Sec: 30

How to print the date in DD-MM-YYYY format:

We can use the above process to print the date in any format. Let me show you how to print it in DD-MM-YYYY format:

#include <stdio.h>
#include <time.h>
int main()
{
	time_t t;
	struct tm *time_data;

	time(&t);
	time_data = localtime(&t);

	printf("Current date in DD-MM-YYYY format: %d-%d-%d", time_data->tm_mday, time_data->tm_mon + 1, time_data->tm_year + 1900);
}

It will print output as below:

Current date in DD-MM-YYYY format: 17-9-2022

How to print the current time in HH:MM:SS format:

Similar to the above example, we can print the time in any format we want.

#include <stdio.h>
#include <time.h>
int main()
{
	time_t t;
	struct tm *time_data;

	time(&t);
	time_data = localtime(&t);

	printf("Current time in HH:MM:SS format: %d:%d:%d", time_data->tm_hour, time_data->tm_min, time_data->tm_sec);
}

It will print:

Current time in HH:MM:SS format: 13:29:24

C example print current date-time in different format

You might also like: