Parsing is usually done in streams, not in strings, but you can use stringstream .
std::istringstream date_s( "04\\10\\1984" ); struct tm date_c; date_s >> std::get_time( &date_c, "%d\\%m\\%Y" ); std::time_t seconds = std::mktime( & date_c );
Now you can compare seconds using < to determine what happened before.
Note std::get_time is new in C ++ 11. It is defined in terms of strptime , which strptime from POSIX but is not part of the C99 standard. You can use strptime if the C ++ 11 library is not available. If you're brave, you can also use the std::time_get ... it's ugly though.
If you do not want to know anything about dates other than the previous ones, you can use std::lexicographical_compare . It will be single-line, but the function name will be so long.
// return true if the date string at lhs is earlier than rhs bool date_less_ddmmyyyy( char const *lhs, char const *rhs ) { // compare year if ( std::lexicographical_compare( lhs + 6, lhs + 10, rhs + 6, rhs + 10 ) ) return true; if ( ! std::equal( lhs + 6, lhs + 10, rhs + 6 ) ) return false; // if years equal, compare month if ( std::lexicographical_compare( lhs + 3, lhs + 5, rhs + 3, rhs + 5 ) ) return true; if ( ! std::equal( lhs + 3, lhs + 5, rhs + 3 ) ) return false; // if months equal, compare days return std::lexicographical_compare( lhs, lhs + 2, rhs ); }
See also how to convert date and time in unix timestamp to c? .
source share