Using boost :: date, how can I calculate "last Monday"?
Here's how to get "two days ago" with 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 do I calculate a date representing "last Monday"?
+3
source to share
1 answer
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 an argument, since "last Monday" will probably never mean "today" on Monday (see docs ). It is also a shorter route to reaching the starting point "N days ago".
+3
source to share