How to get the current date and time in C++ in various formats

Obtaining the current date and time is crucial in many programs. The C++ STL provides the <chrono> library which can be used with <ctime> from c to get the current time in programs in various formats.

Obtaining the time data:

The <chrono> provides three types of clock classes and in this article, the std::chrono::system::clock is used to obtain the time details. This class contains a now() function which returns the details of current time in terms of another class template of the Chrono namespace called timepoint. However, using the auto functionality for complex data types is always easier.

Getting time in “YYYY-MM-DD HH:MM: SS” format in C++

The time point object can be directly used with std::cout to obtain time in this format.

#include <iostream>
#include <chrono>
int main()
{
  auto r=std::chrono::system_clock::now(); //Contains data about current time
  
  std::cout <<r<< "\n";
  
}
Output:
2022-11-28 11:43:04.2836298

Customizing time format in C++

Customizing time format in c++ requires extra effort and the use of <ctime> library.

Every clock in std::chrono has a function to cast its time to “time_t type” called to_time_t(). This time_t is used by the ctime() function of <ctime> to produce a c-style string. This c-style string is used to initialize a C++ string. Then a class called “tme” is created to extract all the details from the string created which can be used as required.

The following code illustrates the point:

#include <iostream>
#include <chrono>
#include<ctime>
#include <format>
class tme
{
public:
  tme(std::string& a)          // extracts the time date for further customization
    :day{a.substr(0,3),a.substr(8,2)},month{a.substr(4,3)},year{a.substr(20,4)}
  {
    tie = a.substr(11, 8);

  }
  

  std::string day[2]{}; //has day details 
 std::string month{}; // month details
 std::string year{}; //year details
  std::string tie{}; //time details
};
int main()
{
  auto r=std::chrono::system_clock::now();
  auto rp=std::chrono::system_clock::to_time_t(r);
  std::string h(ctime(&rp)); //converting to c++ string
  tme curtime(h);   // creating a tme object 
  
}

 

The members of tme class are public. Therefore, it can be accessed in the required way.

Obtaining time in various formats:

We will now see each of the formats one by one with the code snippet.

In Month Date Year and time:

#include <iostream> 
#include <chrono>
#include <ctime>
#include <format>
class tme{ //same as previous code

};
int main()
{
  auto r=std::chrono::system_clock::now();
  auto rp=std::chrono::system_clock::to_time_t(r);
  std::string h(ctime(&rp)); //converting to c++ string
  tme curtime(h);   // creating a tme object 
  std::cout << std::format("{} {} {} {}", curtime.month, curtime.day[1], curtime.year,curtime.tie);

}
Output:
Nov 28 2022 17:47:39

Only date with month and year:

#include <iostream>
#include <chrono>
#include <ctime>
#include <format>
class tme
{
public:
  tme(std::string& a)   
    :day{a.substr(0,3),a.substr(8,2)},month{a.substr(4,3)},year{a.substr(20,4)}
  {
    tie = a.substr(11, 8);

  }
  

  std::string day[2]{}; std::string month{}; std::string year{};
  std::string tie{};
};

int main()
{
  auto r=std::chrono::system_clock::now();
  auto rp=std::chrono::system_clock::to_time_t(r);
  std::string h(ctime(&rp)); //converting to c++ string
  tme curtime(h);   // creating a tme object 
  std::cout << std::format("{} {} {}", curtime.month, curtime.day[1], curtime.year);

}
Output:
Nov 28 2022

Only time:

 #include <iostream>
 #include <chrono>
 #include <ctime> 
class tme
{
public:
  tme(std::string& a)          // extracts the time date for further customization
    :day{a.substr(0,3),a.substr(8,2)},month{a.substr(4,3)},year{a.substr(20,4)}
  {
    tie = a.substr(11, 8);

  }
  

  std::string day[2]{}; std::string month{}; std::string year{};
  std::string tie{};
};

int main()
{
  auto r=std::chrono::system_clock::now();
  auto rp=std::chrono::system_clock::to_time_t(r);
  std::string h(ctime(&rp)); //converting to c++ string
  tme curtime(h);   // creating a tme object 
  std::cout << curtime.tie;

}
Output:
17:56:07

Conclusion:

The time data can be customized easily by extracting the time data from the output string. In this article, a class constructor was used for this purpose. However, the use of std::format() in the article can be replaced by normal std::cout if the compiler doesn’t support this feature.

Leave a Reply

Your email address will not be published. Required fields are marked *