Calculating Ordinal Dates

Explore how to calculate ordinal dates with the chrono library.

As a last example of the new calendar facility, I want to present the online resource Examples and Recipes from Howard Hinnant, which has about 40 examples of the new chrono functionality. Presumably, the chrono extension in C++20 is not easy to get, therefore it’s quite important to have so many examples. You should use these examples as a starting point for further experiments to sharpen your understanding. You can also add your recipes.

To get an idea of Examples and Recipes I want to present a program by Roland Bock that calculates ordinal dates.

An ordinal date consists of a year and a day of year (1st of January being day 1, 31st of December being day 365 or day 366). The year can be obtained directly from year_month_day. And calculating the day is wonderfully easy. In the code below we make us of the fact that year_month_day can deal with invalid dates like the 0th of January:” (Roland Bock)

I added the necessary headers to Roland’s program.

Get hands-on with 1400+ tech skills courses.