ARTICLE AD BOX
To get the dates of Monday and Friday, you can do it like so by adapting the logic given by Howard Hinnant's answer here:
auto todayDate = date::floor<date::days>(std::chrono::system_clock::now()); std::cout << date::format("%F", todayDate) << "\n"; auto mondayDate = todayDate - (date::weekday{ todayDate } - date::Monday); std::cout << date::format("%F", mondayDate) << "\n"; auto fridayDate = mondayDate + (mondayDate - date::Thursday); std::cout << date::format("%F", fridayDate) << "\n";If you need the Unix timestamp version:
auto mondayTimestamp = mondayDate.time_since_epoch(); auto mondayDateSeconds = std::chrono::duration_cast<std::chrono::seconds>(mondayTimestamp).count(); auto fridayTimestamp = fridayDate.time_since_epoch(); auto fridaySeconds = std::chrono::duration_cast<std::chrono::seconds>(fridayTimestamp).count();This should also work for the new C++20 <chrono> library as essentially it's Howard's library underneath
1 Comment
auto fridayDate = mondayDate + (mondayDate - date::Thursday); looks wrong to me. Shouldn't that be auto fridayDate = mondayDate + (date::Friday - mondayDate); instead? Or simpler, you know how many days are between Monday and Friday, and we're not likely to add new weekdays to the calendar anytime soon, so just hard-code it: auto fridayDate = mondayDate + 4;
2026-03-22T18:20:26.497Z+00:00
Explore related questions
See similar questions with these tags.
