In this article we will discuss how to add or subtract days, months & Years from a date object.
Add / Subtract Days from a Date
For example, if current date is 21 March 2016. Then subtracting 100 days from this date will make the date 12 Dec 2015
For this we will use type boost::gregorian::days to represent days i.e.
// Initialize days object with 10 days boost::gregorian::days daysObj(100);
Now we can add or subtract this days object from the date object. So, lets subtract 10 days from a Date object i.e.
// Initializing Date object with 21st March 2016 boost::gregorian::date dateObj { 2016, 3, 21 }; // Subtract 10 days from date object dateObj = dateObj - daysObj;
It will make the date to point to 12 Dec 2015.
Add / Subtract Months from a Date
Adding 2 months to date 12 Dec 2015 will make the date 12 Feb 2016
Frequently Asked:
- Calculate Difference between 2 Dates in C++ using Boost Library
- C++ Date class – Tutorial & Example using Boost Date Time Library
- How to Add / Subtract Days, Months or Years from a Date in C++
- How to Convert String to Date in C++ using Boost Library
For this we will use type boost::gregorian::months to represent days i.e.
// Initialize days object with 2 Months boost::gregorian::months monthObj(2);
Now we can add or subtract this from the date object. So, lets add 2 months in the date object i.e.
// Add 2 Months in date object boost::gregorian::date dateObj2 = dateObj + monthObj;
It will make the date to point to 12 Feb 2016.
Add / Subtract Years from a Date
Subtracting 3 Years to date 12 Feb 2016 will make the date 12 Feb 2013
For this we will use type boost::gregorian::years to represent days i.e.
// Initialize days object with 3 Years boost::gregorian::years yearsObj(3);
Now we can add or subtract this from the date object. So, lets subtract 2Â months in the date object i.e.
// Subtract 3 Years from date object boost::gregorian::date dateObj2 = dateObj - yearsObj;
It will make the date to point to 12 Feb 2013.
Complete Example
Let’s see the complete example,
#include <boost/date_time/gregorian/gregorian.hpp> #include <iostream> int main() { // Initializing Date object with 21st March 2016 boost::gregorian::date dateObj { 2016, 3, 21 }; // Printing complete Date object on console std::cout << "Initial Date = " << dateObj << '\n'; // Initialize days object with 10 days boost::gregorian::days daysObj(100); // Subtract 10 days from date object dateObj = dateObj - daysObj; // Printing complete Date object on console std::cout << "Date After Subtracting " << daysObj << " days = " << dateObj<< '\n'; // Initialize days object with 2 Months boost::gregorian::months monthObj(2); // Add 2 Months in date object dateObj = dateObj + monthObj; // Printing complete Date object on console std::cout << "Date After Adding " << monthObj.number_of_months() << " Months = " << dateObj << '\n'; // Initialize days object with 3 Years boost::gregorian::years yearsObj(3); // Subtract 3 Years from date object dateObj = dateObj - yearsObj; // Printing complete Date object on console std::cout << "Date After Subtracting " << yearsObj.number_of_years() << " Years = " << dateObj << '\n'; }
Output:
Initial Date = 2016-Mar-21 Date After Subtracting 100 days = 2015-Dec-12 Date After Adding 2 Months = 2016-Feb-12 Date After Subtracting 3 Years = 2013-Feb-12
To compile this example use following command in linux,
g++ –std=c++11 example.cpp -lboost_date_time