12345678910111213141516
#include <iostream> #include <sstream> int main() { std::string date{ "12/15/2015" }; std::istringstream ss(date); double month{}; double day{}; double year{}; char junk{}; ss >> month >> junk >> day >> junk >> year; return 0; }
1234567891011121314151617181920
#include <iostream> #include <iomanip> #include <string> int main() { std::string date{ "12/15/2015" }; double dDate{}; date.erase(date.begin() + 2); date.erase(date.begin() + 4); std::cout << '\n' << date; dDate = std::stod(date); std::cout << std::fixed << std::showpoint << std::setprecision(7); std::cout << '\n' << dDate; return 0; }
12345678910111213141516171819202122232425262728
#include <iostream> #include <string> #include <sstream> int date_to_int( const std::string& date_str ) { std::istringstream stm(date_str) ; int mm, dd, yyyy ; char sep1, sep2 ; // if the pattern (int) / (int) / (int) is matched if( stm >> mm >> sep1 >> dd >> sep2 >> yyyy && sep1 == '/' && sep2 == '/' ) { // if required, validate that the values yyyy, mm and dd form a valid date yyyy -= 1900 ; // subtract 1900 from the year return yyyy*10000 + mm*100 + dd ; // form the int and return it } else return 0 ; // badly formed date string } double date_to_dbl( const std::string& date_str ) { return date_to_int(date_str) ; } int main() { std::cout << date_to_int( "12/25/2015" ) << '\n' << std::fixed << date_to_dbl( "12/25/2015" ) << '\n' ; }