How to find out the date of the first day of week from the week number in C++

Using Howard Hinnant’s free, open-source, header-only date library, it can look like this:

#include "date.h"
#include "iso_week.h"
#include <iostream>

int
main()
{
    using namespace iso_week::literals;
    std::cout << date::year_month_day{2017_y/8_w/mon} << '\n';
    std::cout << date::year_month_day{2017_y/10_w/mon} << '\n';
}

which outputs:

2017-02-20
2017-03-06

There are also getters for year, month and day on the year_month_day types, and plenty of formatting options.

Leave a Comment