{"id":9384,"date":"2024-04-08T10:51:22","date_gmt":"2024-04-08T10:51:22","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9384"},"modified":"2024-04-08T10:51:22","modified_gmt":"2024-04-08T10:51:22","slug":"c20-creating-calendar-dates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c20-creating-calendar-dates\/","title":{"rendered":"C++20: Creating Calendar Dates"},"content":{"rendered":"\n<p>In this post, I will dive deeper into calendar dates and create them.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"411\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage.png\" alt=\"\" class=\"wp-image-9278\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage.png 960w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-768x329.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-705x302.png 705w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>If this post is too overwhelming for you, read my previous ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology\/\">Basic Chrono Terminology<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology-with-time-duration-and-time-point\/\">Basic Chrono Terminology with Time Duration and Time Point<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-time-of-day\/\">Time of Day: Details<\/a><\/li>\n<\/ul>\n\n\n\n<p>C++20 supports constants and literals to make using calendar-date types more convenient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Constants and Literals for Calendar Types<\/h2>\n\n\n\n<p>Let me start with the constants for<code> std::chrono::weekday<\/code>, and <code>std::chrono::month<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>std::chrono::weekday<\/code><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Monday\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Thuesday\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Wednesday\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Thursday\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Friday\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Saturday\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Sunday\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>std::chrono::month<\/code><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>January\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>February\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>March\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>April\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>May\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>June\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>July\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>August\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>September\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>October\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>November\nstd<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>December\n<\/pre><\/div>\n\n\n\n<p>C++20 supports for calendar types <code>std::chrono::day<\/code> and <code>std::chrono::year<\/code> two new literals: <code>d<\/code> and <code>y<\/code>. You can read more details about it in the post <a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology-with-time-duration-and-time-point\/\">Basic Chrono Terminology with Time Duration and Time Point<\/a>.<\/p>\n\n\n\n<p>Let me create a few calendar dates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create Calendar Dates<\/h2>\n\n\n\n<p>The program <code>createCalendar.cpp<\/code> shows various ways to create calendar-related dates.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ createCalendar.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n \n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> std<span style=\"color: #555555\">::<\/span>chrono_literals; \n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>last;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>day;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day_last;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_weekday;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_weekday_last;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month_weekday;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month_weekday_last;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month_day;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month_day_last;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>weekday_last;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>weekday;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>January;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>February;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>June;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>March;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>October;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Monday;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Thursday;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Sunday;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> yearMonthDay{year(<span style=\"color: #FF6600\">1940<\/span>)<span style=\"color: #555555\">\/<\/span>month(<span style=\"color: #FF6600\">6<\/span>)<span style=\"color: #555555\">\/<\/span>day(<span style=\"color: #FF6600\">26<\/span>)};                <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> yearMonthDay <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> year_month_day(<span style=\"color: #FF6600\">1940<\/span>y, June, <span style=\"color: #FF6600\">26<\/span>d) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                   <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> yearMonthDayLast{year(<span style=\"color: #FF6600\">2010<\/span>)<span style=\"color: #555555\">\/<\/span>March<span style=\"color: #555555\">\/<\/span>last};                  <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> yearMonthDayLast <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> year_month_day_last(<span style=\"color: #FF6600\">2010<\/span>y, month_day_last(month(<span style=\"color: #FF6600\">3<\/span>))) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> yearMonthWeekday{year(<span style=\"color: #FF6600\">2020<\/span>)<span style=\"color: #555555\">\/<\/span>March<span style=\"color: #555555\">\/<\/span>Thursday[<span style=\"color: #FF6600\">2<\/span>]};            <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> yearMonthWeekday <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> year_month_weekday(<span style=\"color: #FF6600\">2020<\/span>y, month(March), Thursday[<span style=\"color: #FF6600\">2<\/span>]) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> yearMonthWeekdayLast{year(<span style=\"color: #FF6600\">2010<\/span>)<span style=\"color: #555555\">\/<\/span>March<span style=\"color: #555555\">\/<\/span>Monday[last]};        <span style=\"color: #0099FF; font-style: italic\">\/\/(5)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> yearMonthWeekdayLast <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> year_month_weekday_last(<span style=\"color: #FF6600\">2010<\/span>y, month(March), weekday_last(Monday));\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> day_{day(<span style=\"color: #FF6600\">19<\/span>)};     <span style=\"color: #0099FF; font-style: italic\">\/\/ (6)         <\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> day_  <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> day(<span style=\"color: #FF6600\">19<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> month_{month(<span style=\"color: #FF6600\">1<\/span>)};  <span style=\"color: #0099FF; font-style: italic\">\/\/ (7)     <\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> month_  <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> month(<span style=\"color: #FF6600\">1<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> year_{year(<span style=\"color: #FF6600\">1988<\/span>)}; <span style=\"color: #0099FF; font-style: italic\">\/\/ (8)    <\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> year_  <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> year(<span style=\"color: #FF6600\">1988<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> weekday_{weekday(<span style=\"color: #FF6600\">5<\/span>)};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> weekday_  <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> weekday(<span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n \n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> yearMonth{year(<span style=\"color: #FF6600\">1988<\/span>)<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">1<\/span>};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> yearMonth  <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> year_month(year(<span style=\"color: #FF6600\">1988<\/span>), January) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n \n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> monthDay{<span style=\"color: #FF6600\">10<\/span><span style=\"color: #555555\">\/<\/span>day(<span style=\"color: #FF6600\">22<\/span>)};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> monthDay <span style=\"color: #555555\">&lt;&lt;<\/span>  <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> month_day(October, day(<span style=\"color: #FF6600\">22<\/span>)) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> monthDayLast{June<span style=\"color: #555555\">\/<\/span>last};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> monthDayLast <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> month_day_last(month(<span style=\"color: #FF6600\">6<\/span>)) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n \n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> monthWeekday{<span style=\"color: #FF6600\">2<\/span><span style=\"color: #555555\">\/<\/span>Monday[<span style=\"color: #FF6600\">3<\/span>]};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> monthWeekday <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> month_weekday(February, Monday[<span style=\"color: #FF6600\">3<\/span>]) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n \n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> monthWeekDayLast{June<span style=\"color: #555555\">\/<\/span>Sunday[last]};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> monthWeekDayLast <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> month_weekday_last(June, weekday_last(Sunday)) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>There are two ways to create a calendar date. You can use the so-called cute syntax <code>yearMonthDay{year(1940)\/month(6)\/day(26)}<\/code> (line 1), or you can use the explicit type <code>date::year_month_day(1940y, June, 26d<\/code>) (line 2). To avoid overwhelming you, I will delay my explanation of the cute syntax to the next section. The explicit type is interesting because it uses the date-time literals <code>1940y<\/code>, <code>26d<\/code>, and the predefined constant <code>June<\/code>. This was the obvious part of the program.<\/p>\n\n\n\n<p>Line 3, line 4, and line 5 offer additional ways to create calendar dates.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Line 3: the last day of March 2010: <code>{year(2010)\/March\/last}<\/code> or <code>year_month_day_last(2010y,month_day_last(month(3)))<\/code><\/li>\n\n\n\n<li>Line 4: the second Thursday of March 2020:<code> {year(2020)\/March\/Thursday[2]} <\/code>or<code> year_month_weekday(2020y, month(March), Thursday[2])<\/code><\/li>\n\n\n\n<li>Line 5: the last Monday of March 2010: <code>{year(2010)\/March\/Monday[last]} <\/code>or<code> year_month_weekday_last(2010y, month(March), weekday_last(Monday))<\/code><\/li>\n<\/ul>\n\n\n\n<p>The remaining calendar types stand for a day (line 6), a month (line 7), or a year (line 8). You can combine them as basic building blocks for fully specified calendar dates, such as in lines 3, 4, or 5.<\/p>\n\n\n\n<p>This is the output of the program:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"1030\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-1030x1030.png\" alt=\"\" class=\"wp-image-9397\" style=\"width:500px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-1030x1030.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-300x300.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-80x80.png 80w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-768x769.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-36x36.png 36w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-180x180.png 180w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar-705x705.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/createCalendar.png 1126w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p>As promised, let me write about the cute syntax.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cute Syntax<\/h2>\n\n\n\n<p>The cute syntax consists of overloaded division operators to specify a calendar date. The overloaded operators support time literals (e.g., <code>2020y<\/code>, <code>31d<\/code>) and <code>std::chrono::month<\/code> &nbsp;constants such as <code>std::chrono::January<\/code>, <code>std::chrono::February<\/code>, &#8230;, &nbsp;<code>std::chrono::December<\/code>.<\/p>\n\n\n\n<p>The following three combinations of year, month, and day are possible using the cute syntax.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">year<span style=\"color: #555555\">\/<\/span>month<span style=\"color: #555555\">\/<\/span>day\nday<span style=\"color: #555555\">\/<\/span>month<span style=\"color: #555555\">\/<\/span>year\nmonth<span style=\"color: #555555\">\/<\/span>day<span style=\"color: #555555\">\/<\/span>year\n<\/pre><\/div>\n\n\n\n<p>These combinations are not chosen arbitrarily. They are the ones used most worldwide, and no other combination is allowed.<\/p>\n\n\n\n<p>Consequently, when you choose the type <code>year<\/code>, <code>month<\/code>, or <code>day<\/code> for the first argument, the type for the remaining two arguments is no longer necessary, and a number does the job.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ cuteSyntax.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> yearMonthDay{std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year(<span style=\"color: #FF6600\">1966<\/span>)<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">6<\/span><span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">26<\/span>};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> yearMonthDay <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> dayMonthYear{std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>day(<span style=\"color: #FF6600\">26<\/span>)<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">6<\/span><span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">1966<\/span>};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> dayMonthYear <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> monthDayYear{std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month(<span style=\"color: #FF6600\">6<\/span>)<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">26<\/span><span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">1966<\/span>};\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> monthDayYear <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    constexpr <span style=\"color: #006699; font-weight: bold\">auto<\/span> yearDayMonth{std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year(<span style=\"color: #FF6600\">1966<\/span>)<span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month(<span style=\"color: #FF6600\">26<\/span>)<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">6<\/span>};  \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> yearDayMonth <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n<\/pre><\/div>\n\n\n\n<p>The last values for year\/day\/month are not allowed and cause a run-time message.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"511\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/cuteSyntax-1030x511.png\" alt=\"\" class=\"wp-image-9417\" style=\"width:450px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/cuteSyntax-1030x511.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/cuteSyntax-300x149.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/cuteSyntax-768x381.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/cuteSyntax-705x350.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/cuteSyntax.png 1126w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next<\/h2>\n\n\n\n<p>I assume you want to display a calendar date <code>{year(2010)\/March\/last}<\/code> in a readable form, for example, 2020-03-31. This is a job for the <code>local_days<\/code> or <code>sys_days<\/code> operator.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I will dive deeper into calendar dates and create them. If this post is too overwhelming for you, read my previous ones: C++20 supports constants and literals to make using calendar-date types more convenient. Constants and Literals for Calendar Types Let me start with the constants for std::chrono::weekday, and std::chrono::month. std::chrono::Monday std::chrono::Thuesday [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":9278,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-9384","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-time"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=9384"}],"version-history":[{"count":26,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9384\/revisions"}],"predecessor-version":[{"id":9440,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9384\/revisions\/9440"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/9278"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}