这是使用boost::date
:
boost::gregorian::date today = boost::gregorian::day_clock::local_day();
boost::date_time::day_functor<boost::gregorian::date> day_offset(-2);
boost::gregorian::date modified = today + day_offset.get_offset(today);
如何计算表示"上周一"的日期?
使用previous_weekday:
using namespace boost::gregorian;
auto last_monday = previous_weekday(today-days(1), greg_weekday(Monday));
Edit:添加-days(1)
以避免返回作为参数给出的日期,因为"last Monday"在星期一可能永远不会意味着"today"(参见文档)。这也是完成"N天前"起点的一种更短的方式。