In this article we will discuss how to convert string to date objects in C++ using Boost Date Time Library.

Boost Date Time Library provides boost::gregorian::date type for all the calendar date related operations. But many times we encounter scenarios where we need to convert parse date from some text and then convert date in string format to Date object for further operations.

For example, we might get date in following string formats,

2016/10/30 —- YYYY/MM/DD
2016/30/10 —- YYYY/DD/MM
30-10-2016 —- DD-MM-YYYY
10 30 2016 —- MM DD YYYY

Alignment of Month, Date, year and Delimiter may vary from scenario to scenario. Let’s see simple ways to convert string to boost::gregorian::date objects using some factory functions i.e.

boost::gregorian::date_from_iso_string();
boost::gregorian::from_simple_string();
boost::gregorian::from_us_string();
boost::gregorian::from_uk_string();

Delimiters

In all the above factory functions following delimiters can be – ‘,’,’-‘,’.’,’ ‘,’/’,’\0′ used to seperate months,days & Years.
For example, for these factory functions “YYYY-MM-DD” is equivalent to “YYYY/MM/DD” , “YYYY,MM.DD” & “YYYY MM DD”

Converting ISO Format String (YYYYMMDD) to Date Object

Boost provides boost::gregorian::date_from_iso_string that converts ISO Format String date to date object i.e.

// Converting ISO Format (YYYYMMDD) String to date object
date d1 = date_from_iso_string("20160221");

Converting Simple Format (YYYY-MM-DD) String to date object

Boost provides boost::gregorian::from_simple_string that converts Simple Format String date to date object i.e.

// Converting Simple Format (YYYY-MM-DD) String to date object
date d2_1 = from_simple_string("2016-2-21");
// Converting Simple Format (YYYY MM DD) String to date object
date d2_2 = from_simple_string("2016 2 21");
// Converting Simple Format (YYYY/MM/DD) String to date object
date d2_3 = from_simple_string("2016/2/21");
// Converting Simple Format (YYYY.MM.DD) String to date object
date d2_4 = from_simple_string("2016.2.21");

Converting US Format (MM-DD-YYYY) String to date object

Boost provides boost::gregorian::from_us_string that converts US Format String date to date object i.e.

// Converting US Format (MM-DD-YYYY) String to date object
date d3_1 = from_us_string("02-21-2016");
// Converting US Format (MM/DD/YYYY) String to date object
date d3_2 = from_us_string("02/21/2016");
// Converting US Format (MM DD YYYY) String to date object
date d3_3 = from_us_string("02 21 2016");
// Converting US Format (MM,DD.YYYY) String to date object
date d3_4 = from_us_string("02,21.2016");

Converting UK Format (DD-MM-YYYY) String to date object

Boost provides boost::gregorian::from_uk_string that converts UK Format String date to date object i.e.

// Converting UK Format (DD-MM-YYYY) String to date object
date d4_1 = from_uk_string("21-02-2016");
// Converting UK Format (DD/MM/YYYY) String to date object
date d4_2 = from_uk_string("21/02/2016");
// Converting UK Format (DD MM YYYY) String to date object
date d4_3 = from_uk_string("21 02 2016");
// Converting UK Format (DD,MM.YYYY) String to date object
date d4_4 = from_uk_string("21,02.2016");

Instead of Month number in the string we can also use month name with all the above factory functions i.e.
date d5 = from_uk_string("21-FEBRUARY-2016");
date d6 = from_uk_string("21-FEB-2016");

Complete example is as follows,
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

using namespace boost::gregorian;

int main() {

	// . / - ' '
	// Converting ISO Format (YYYYMMDD) String to date object
	date d1 = date_from_iso_string("20160221");

	std::cout << d1 << std::endl;

// Converting Simple Format (YYYY-MM-DD) String to date object
	date d2_1 = from_simple_string("2016-2-21");
// Converting Simple Format (YYYY MM DD) String to date object
	date d2_2 = from_simple_string("2016 2 21");
// Converting Simple Format (YYYY/MM/DD) String to date object
	date d2_3 = from_simple_string("2016/2/21");
// Converting Simple Format (YYYY.MM.DD) String to date object
	date d2_4 = from_simple_string("2016.2.21");

	std::cout << d2_1 << std::endl;
	std::cout << d2_2 << std::endl;
	std::cout << d2_3 << std::endl;
	std::cout << d2_4 << std::endl;

// Converting US Format (MM-DD-YYYY) String to date object
	date d3_1 = from_us_string("02-21-2016");
// Converting US Format (MM/DD/YYYY) String to date object
	date d3_2 = from_us_string("02/21/2016");
// Converting US Format (MM DD YYYY) String to date object
	date d3_3 = from_us_string("02 21 2016");
// Converting US Format (MM,DD.YYYY) String to date object
	date d3_4 = from_us_string("02,21.2016");

	std::cout << d3_1 << std::endl;
	std::cout << d3_2 << std::endl;
	std::cout << d3_3 << std::endl;
	std::cout << d3_4 << std::endl;

// Converting UK Format (DD-MM-YYYY) String to date object
	date d4_1 = from_uk_string("21-02-2016");
// Converting UK Format (DD/MM/YYYY) String to date object
	date d4_2 = from_uk_string("21/02/2016");
// Converting UK Format (DD MM YYYY) String to date object
	date d4_3 = from_uk_string("21 02 2016");
// Converting UK Format (DD,MM.YYYY) String to date object
	date d4_4 = from_uk_string("21,02.2016");

	std::cout << d4_1 << std::endl;
	std::cout << d4_2 << std::endl;
	std::cout << d4_3 << std::endl;
	std::cout << d4_4 << std::endl;

	// Converting UK Format (DD-<MONTH>-YYYY) String to date object
	date d5 = from_uk_string("21-FEBRUARY-2016");
	std::cout << d5 << std::endl;

	// Converting UK Format (DD-<MONTH>-YYYY) String to date object
	date d6 = from_uk_string("21-FEB-2016");
	std::cout << d6 << std::endl;

	return 0;
}

Output:
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21
2016-Feb-21

To compile the above code use following command in linux,

g++ –std=c++11 example.cpp -lboost_date_time

Join a list of 2000+ Programmers for latest Tips & Tutorials