Print System Time in C++



In C++, the standard library does not provide any proper built-in date type. Instead of that we use structures and functions to inherit from the C language for the manipulation.

To access this date and time related functions and structures, we need to include <ctime> header file to the C++ program.

There are four time-related types: clock_t, time_t, size_t, and tm. The types: clock_t, size_t and time_t are capable of representing the system time and date as some sort of integer.

  • time_t: It is used to store calendar time (like the number of seconds since Jan 1, 1970).
  • clock_t:It is used for measuring processor time (not calendar time).
  • size_t:It is used for representing sizes (used in loops and memory allocation).
  • tm: A struct that holds human-readable time elements like hour, minute, day, month, etc.

The structure type tm holds the date and time in the form of a C structure having the following elements:

struct tm {
   int tm_sec; // seconds of minutes from 0 to 61
   int tm_min; // minutes of hour from 0 to 59
   int tm_hour; // hours of day from 0 to 24
   int tm_mday; // day of month from 1 to 31
   int tm_mon; // month of year from 0 to 11
   int tm_year; // year since 1900
   int tm_wday; // days since sunday
   int tm_yday; // days since January 1st
   int tm_isdst; // hours of daylight savings time
}

Basic Way Using ctime()

Suppose you want to retrieve the current system date and time as a local time.Below is the example to achieve this:

Example

In this example, we fetch the current date and time from the system as a local time that converts it into a readable string format.

#include<iostream>
#include<ctime>
using namespace std;
int main() {
   // current date/time based on current system
   time_t now = time(0);
   char* dt = ctime(&now); // convert now to string form
   cout<<"The local date and time is: "<<dt<<endl;
}

The above code produces the following result:

The local date and time is: Fri Apr 25 18:31:34 2025

Structured Format Using tm and asctime()

Here, we want to retrieve the current system date and time as a Coordinated Universal Time (UTC).

Example

In this example, we fetch the current date and time from the system as a UTC that converts it into a readable string format.

#include<iostream>
#include<ctime>
using namespace std;
int main() {
   time_t now = time(0);
   char* dt = ctime(&now);
   // convert now to tm struct for UTC
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout<<"The UTC date and time is:"<<dt<<endl;
}

The above code produces the following result:

The UTC date and time is:Fri Apr 25 13:00:53 2025

Custom Format Using strftime()

This is the most flexible way and we can customize the Time and Date format as (like DD-MM-YYYY HH:MM:SS).

Example

In this example, we format the current system time into a custom date-time format as (DD-MM-YYYY HH:MM:SS).

#include<iostream>
#include<ctime>
using namespace std;
int main() {
   time_t now = time(0);                
   tm* localTime = localtime(&now); 
   char buffer[80];
   strftime(buffer, sizeof(buffer), "%d-%m-%Y %H:%M:%S", localTime);
   cout<<"Custom Formatted Time: "<<buffer<<endl;
   return 0;
}

The above code produces the following result:

Custom Formatted Time: 25-04-2025 17:42:52
Updated on: 2025-04-28T18:34:23+05:30

592 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements