Change date format in C++

Hello, Coders! In this section, we will learn about various date formats and how to change them in C++.

Date Formats Type

There are various types of date formats that we can use in our programs. Some of those most used date formats are mentioned below.

  • MM/DD/YY: Month/Day/Year including leading zeros. (11/19/2021)
  • DD/MM/YY: Day/Month/Year including leading zeros. (19/11/2021)
  • YY/MM/DD: Year/Month/Day including leading zeros. (2021/11/19)
  • Month D, Y: Month name Day, Year including no leading zeros. (November 19, 2021)
  • YYMMDD: YearMonthDate with no separators. (20211119)
  • YY-MM-DD: Year-Month-Day, with dashes as the separators. (2021-11-19)

strftime() function in C++

We use strftime() function in C/C++ for formatting date and time in a program. The function is declared in ctime standard file of the C++ library.

Syntax:

size_t strftime(char8 ptr, size_t max_size, const char* format, const struct tm* timeptr);

Here,

  • ptr : Pointer to the destination array.
  • max_size: The Maximum number of characters to be copied.
  • format: These are the format specifies to represent the time in tm.
  • timeptr: Pointer to tm structure.

Let’s discuss some of the strftime() format specifiers, which will help us for changing the date format later.

  • %YIt prints 4 digits of the year. (e.g. 2021)
  • %yIt prints the last 2 digits of the year. (e.g. 21)
  • %bIt prints the name of the month name. (e.g. Nov)
  • %BIt prints the full name of the month. (e.g. November)
  • %m It prints the month as a decimal number. (e,g, 11)
  • %dIt prints day of the month as a decimal number. (e.g. 19)

Program to change The Date Format in C++

We can change the format of the date according to our requirements by using the different format specifier.

Example:

#include <ctime>
#include <iostream>
using namespace std;

int main()
{
  time_t currTime;
  tm* currTm;
  
  char dateString1[100];
  char dateString2[100];
  char dateString3[100];
  char dateString4[100];
  char dateString5[100];
  time(&currTime);
  
  currTm = localtime(&currTime);
  
  strftime(dateString1, 50, "DD/MM/YY: %d/%m/%Y", currTm);
  strftime(dateString2, 50, "MM/DD/YY: %m/%d/%Y", currTm);
  strftime(dateString3, 50, "YY/MM/DD: %Y/%d/%m", currTm);
  strftime(dateString4, 50, "Month D, Y: %B %d, %Y", currTm);
  strftime(dateString5, 50, "YY-MM-DD: %Y-%m-%d", currTm);
  
  cout << dateString1 << " -> " << dateString2 << " -> " <<
  dateString3 << " -> " << dateString4 << " -> " <<
  dateString5 << endl;
  
  return 0;
}

Output:

DD/MM/YY: 19/11/2021 -> MM/DD/YY: 11/19/2021 -> YY/MM/DD: 2021/19/11 -> Month D, Y: November 19, 2021 -> YY-MM-DD: 2021-11-19

Alternative Method for Changing The Date Format

The previous method is only useful if we have  to change the current system date.  If we have been given with an arbitrary date format as a string, then we use strptime() function to convert the date to any format.

Let’s understand it by an example.

Example:

#include <ctime>
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
  ostringstream date1;
  date1 << 20211119; //Date in YYMMDD format

  struct tm tm;
  strptime(date1.str().c_str(), "%Y%m%d", &tm);

  char date2[50];
  strftime(date2, sizeof(date2), "%d-%b-%Y", &tm);

  std::cout << date1.str() << " -> " << date2 << std::endl;
}

Here we take the date format as YYMMDD and convert it in DD-Month-Year format.

Output:

20211119 -> 19-Nov-2021

Hope this C++ tutorial has helped you to understand the concept of date format and how to change them in a C++ program.

Happy Coding!!!

You can also read, How to change the format of date in Python

Leave a Reply

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