How to Convert String to Date in C++? Last Updated : 22 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, we generally store the date and time data in the form of the object of type std::tm. Sometimes, this data is stored inside a string object and we need to convert it into the tm type for further processing. In this article, we will learn how to convert a string to date in C++. Example: Input: dateString = "2024-03-14"; Output: Date: Thu Mar 14 00:00:00 2024Convert a String to Date in C++To convert a std::string to date in C++, we can use the std::stringstream class along with the std::get_time() function provided in the <iomanip> a library that parses the string stream interpreting its content as a date and time specification to fill the tm structure. Syntax of get_time()ss >> get_time(&tm, "%Y-%m-%d");Here, ss is the string stream to be converted to date."%Y-%m-%d" is the format of the date in the string.tm is the tm structure to store the date.C++ Program to Convert String to DateThe below program demonstrates how we can convert a string to date using std::get_time() function in C++. C++ // C++ program to illustrate how to convert string to date #include <ctime> #include <iomanip> #include <iostream> #include <sstream> using namespace std; int main() { // Define a string representing a date string dateString = "2024-03-14"; // Initialize a tm structure to hold the parsed date tm tm = {}; // Create a string stream to parse the date string istringstream ss(dateString); // Parse the date string using std::get_time ss >> get_time(&tm, "%Y-%m-%d"); // Check if parsing was successful if (ss.fail()) { cout << "Date parsing failed!" << endl; return 1; } // Convert the parsed date to a time_t value time_t date = mktime(&tm); // Output the parsed date using std::asctime cout << "Date: " << asctime(localtime(&date)); return 0; } OutputDate: Thu Mar 14 00:00:00 2024 Time Complexity: O(N), here N is the length of the date string.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Convert String to Date in C++? M mguru4c05q Follow Improve Article Tags : C++ Programs C++ cpp-string date-time-program cpp-strings +1 More Practice Tags : CPPcpp-strings Similar Reads How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor 1 min read How to Convert wstring to double in C++ In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou 2 min read How to Convert a Single Character to String in C++? A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co 4 min read Convert char* to std::string in C++ Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char* 3 min read C++ Program For String to Double Conversion There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun 3 min read Like