Convert String to Date in C++
Parse a date string like "25/12/2024" into day, month, and year in C++ with getline on '/' and stoi, then with sscanf. Covers validation and malformed input.
1#include <iostream>2#include <string>3#include <sstream>4#include <ctime>5using namespace std;67struct Date {8 int day;9 int month;10 int year;11};1213Date parseDateString(string dateStr) {14 Date date;15 stringstream ss(dateStr);16 string token;1718 // Parse day19 getline(ss, token, '/');20 date.day = stoi(token);2122 // Parse month23 getline(ss, token, '/');24 date.month = stoi(token);2526 // Parse year27 getline(ss, token, '/');28 date.year = stoi(token);2930 return date;31}3233int main() {34 string dateString = "25/12/2024";3536 // Method 1: Using stringstream37 Date date1 = parseDateString(dateString);3839 cout << "Date string: " << dateString << endl;40 cout << "Parsed date - Day: " << date1.day41 << ", Month: " << date1.month42 << ", Year: " << date1.year << endl;4344 // Method 2: Using sscanf45 int day, month, year;46 sscanf(dateString.c_str(), "%d/%d/%d", &day, &month, &year);47 cout << "\nUsing sscanf - Day: " << day48 << ", Month: " << month49 << ", Year: " << year << endl;5051 return 0;52}
Sample Output
Parsed date - Day: 25, Month: 12, Year: 2024
From the stringstream method.
In this one
- How getline with a delimiter splits a string
- How stoi turns each piece into an int
- How a struct groups related values
- How sscanf parses a pattern in one call
How it works
- 1
dateString holds "25/12/2024".
- 2
parseDateString uses getline(ss, token, '/') three times.
- 3
stoi converts "25", "12", "2024".
- 4
The Date struct is returned and printed.
- 5
sscanf reads the same pattern into three ints.
Edge cases
Wrong separator
Use "25-12-2024".
The first getline reads the whole string and stoi parses 25 then stops at "-". The next getline finds nothing and stoi("") throws invalid_argument.
Impossible date
Use "31/02/2024".
Parsing succeeds because nothing checks the calendar. Validate month 1–12 and the day against the month, including leap years.
What's going on
The program converts a date string (e.g., "25/12/2024") into separate day, month, and year components. For example:
The code shows multiple parsing methods, each with different advantages and use cases.
Header Files Used
Understanding Date String Parsing
Date Formats:
Parsing Challenge:
Date Structure
struct Date {
int day;
int month;
int year;
};
How it works:
Method 1: Using stringstream with getline()
Date parseDateString(string dateStr) {
Date date;
stringstream ss(dateStr);
string token;
// Parse day
getline(ss, token, '/');
date.day = stoi(token);
// Parse month
getline(ss, token, '/');
date.month = stoi(token);
// Parse year
getline(ss, token, '/');
date.year = stoi(token);
return date;
}
How it works:
Step-by-step (for "25/12/2024"):
Method 2: Using sscanf()
int day, month, year;
sscanf(dateString.c_str(), "%d/%d/%d", &day, &month, &year);
How it works:
Advantages:
Disadvantages:
Understanding getline() with Delimiter
getline() Function:
Example:
Other Methods (Mentioned but not shown in code)
Method 3: Using Regex
#include <regex>
regex pattern(R"((\d+)/(\d+)/(\d+))");
smatch matches;
if (regex_match(dateString, matches, pattern)) {
day = stoi(matches[1]);
month = stoi(matches[2]);
year = stoi(matches[3]);
}
Method 4: Manual Parsing
When to Use Each Method
Best Practice: Use stringstream for most cases - it's clear, flexible, and modern C++.
Important Considerations
Date Format Assumptions:
Input Validation:
Error Handling:
Common Use Cases
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does getline(ss, token, '/') read from "25/12/2024" first?
Pick an answer to check it.
Question 2
What does sscanf return on success here?
Pick an answer to check it.
Questions people ask
Which header does sscanf need?
<cstdio>. This listing gets it through other headers on many compilers, but include it yourself to be safe.
Is there a modern date library?
C++20 <chrono> has calendar types like year_month_day.
Why return a struct?
It keeps the three related values together in one return value.