#define BOOST_DATE_TIME_SOURCE
/*#define BOOST_DATE_TIME_SOIURCE #define BOOST_DATE_TIME_NO_LIB或者#BOOST_ALL_NO_LIB
这些宏定义指明项目对于boost库是源代码级别的引入,将boost库源代码直接嵌入到工程中,不加的话默认找编译后的boost库文件
*/
#include<iostream>
#include<libs/date_time/src/gregorian/greg_names.hpp>
#include<libs/date_time/src/gregorian/date_generators.cpp>
#include<libs/date_time/src/gregorian/greg_month.cpp>
#include<libs/date_time/src/gregorian/greg_weekday.cpp>
#include<boost/date_time/gregorian/gregorian.hpp>
using namespace std;
using namespace boost::gregorian;
int main()
{ //日期与days month years weeks类之间的计算
date d1(1991, 5, 1);
date d2(day_clock::local_day());//获得当前日期
cout << d2 - d1 << endl;//日期之间的天数计算
days temp = d2 - d1;
cout << d1 + temp << endl;//日期与day计算
d1 = d1 + days(100);//日期与day计算
cout << d1 << endl;
cout << d1.month() << endl;
cout << d1.day() << endl;
d1 += months(3);//日期与月类计算
cout << d1 << endl;
d1 -= weeks(4);//日期与weeks计算
cout << d1 << endl;
date d3(1991,5,1);
d3 += years(23);//日期与年份计算
cout << d3 << endl;
cout << d3 + days(pos_infin) << endl;//date与特殊日期的计算
cout << d1 + days(not_a_date_time);
cout << d2 - date(neg_infin) << endl;
//使用月份和日期的特殊情况
date d5(2000, 2, 29);
d5 += months(1);//日期变成2000-3-31
cout << d5 << endl;
d5 += months(1);//日期变成2000-4-30
cout << d5 << endl;
date d4(2000, 2, 29);
cout << d4 + years(1) << endl;//日期变成2000-2-28
getchar();
}
上面例子是对date与其他时间类型的基本计算,操作效果如下:

boost准模板库date类使用(续1 date与日期date 周weeks 年份years 天days计算),布布扣,bubuko.com
boost准模板库date类使用(续1 date与日期date 周weeks 年份years 天days计算)
原文:http://blog.csdn.net/onlysingleboy/article/details/23429321