gregorian::date date1 = 2014-12-18;
gregorian::date date2 = 2014-12-19;
gregorian::date currentDate;
if(date1 < date2)
{
date1 = currentDate;
}
else
{
date2 = currentDate;
}
与此类似的功能是我的问题的理想选择,我想知道是否可以比较日期或是否有现有功能这样做?
答案 0 :(得分:2)
关于格里高利日期的文档说明所有比较运算符都可用。见http://www.boost.org/doc/libs/1_35_0/doc/html/date_time/gregorian.html#date_operators
答案 1 :(得分:2)
我认为你不想要这个:
gregorian::date date1 = 1984; // 2014-12-18 = 2014 - 12 - 18 = 1984
gregorian::date date2 = 1983; // 2014-12-19 = 2014 - 12 - 19 = 1983
试着看看:
date(greg_year, greg_month, greg_day)
或
date from_string(std::string)
答案 2 :(得分:2)
当然,您需要正确构建日期,否则比较没有任何问题。
请注意,您需要day_clock
才能获得currentDate
(否则您将获得not-a-datetime
)
<强> Live On Coliru 强>
#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/date_time/gregorian/gregorian_io.hpp>
#include <iostream>
using namespace boost;
int main() {
gregorian::date date1(2014, 12, 18);
gregorian::date date2(2014, 12, 19);
std::cout << date1 << ", " << date2 << "\n";
(date1 < date2 ? date1 : date2) = gregorian::day_clock::local_day();
std::cout << date1 << ", " << date2 << "\n";
}
这将使用当前日期替换最低值,打印
2014-Dec-18, 2014-Dec-19
2014-Dec-19, 2014-Dec-19