4

This is how one gets "two days ago" using 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);

How would one compute the date that represents "last Monday"?

screwnut
  • 1,367
  • 7
  • 26

1 Answers1

5

Use previous_weekday:

using namespace boost::gregorian;
auto last_monday = previous_weekday(today-days(1), greg_weekday(Monday));

Edit: Added -days(1) to avoid returning the date given as argument, as “last Monday” would probably never mean “today” on a Monday (see the docs). This is also a shorter way of accomplishing your “N days ago” starting point.

Émilien Tlapale
  • 903
  • 5
  • 11