{"id":2532,"date":"2017-08-03T10:01:31","date_gmt":"2017-08-03T08:01:31","guid":{"rendered":"http:\/\/mariusbancila.ro\/blog\/?p=2532"},"modified":"2017-08-04T09:24:23","modified_gmt":"2017-08-04T07:24:23","slug":"computing-day-of-year-in-c","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/","title":{"rendered":"Computing day of year in C++"},"content":{"rendered":"<p>I have been recently asked on my post on the <a href=\"https:\/\/github.com\/HowardHinnant\/date\" target=\"_blank\">date<\/a> library if the library has a function for computing the day of the year. It actually does not, although it is fairly simple to compute it.<\/p>\n<p><strong>UPDATE<\/strong>: Howard Hinnant has shown in a comment below how to write a <tt>day_of_year()<\/tt> function using the <tt>date<\/tt> libray.<\/p>\n<p>Let&#8217;s look at the days of the year.<\/p>\n<table class=\"simple\">\n<tr>\n<th>Day<\/th>\n<th>Day of year<\/th>\n<\/tr>\n<tr>\n<td>January 1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>January 2<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<\/tr>\n<tr>\n<td>January 31<\/td>\n<td>31<\/td>\n<\/tr>\n<tr>\n<td>February 1<\/td>\n<td>32<\/td>\n<\/tr>\n<tr>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<\/tr>\n<tr>\n<td>February 28<\/td>\n<td>59<\/td>\n<\/tr>\n<\/table>\n<p>Here is where things complicate a bit, because during leap years February has 29 days. So we actually need to have two counts of days.<\/p>\n<table class=\"simple\">\n<tr>\n<th>Day<\/th>\n<th>Day of non-leap year<\/th>\n<th>Day of leap year<\/th>\n<\/tr>\n<tr>\n<td>January 1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>January 2<\/td>\n<td>2<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<\/tr>\n<tr>\n<td>January 31<\/td>\n<td>31<\/td>\n<td>31<\/td>\n<\/tr>\n<tr>\n<td>February 1<\/td>\n<td>32<\/td>\n<td>32<\/td>\n<\/tr>\n<tr>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<\/tr>\n<tr>\n<td>February 28<\/td>\n<td>59<\/td>\n<td>59<\/td>\n<\/tr>\n<tr>\n<td>February 29<\/td>\n<td>N\/A<\/td>\n<td>60<\/td>\n<\/tr>\n<tr>\n<td>March 1<\/td>\n<td>60<\/td>\n<td>61<\/td>\n<\/tr>\n<tr>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<\/tr>\n<tr>\n<td>December 31<\/td>\n<td>365<\/td>\n<td>366<\/td>\n<\/tr>\n<\/table>\n<p>It is fairly simple to compute the day of the year based on the day of the month if we knew the day of the year of each first day of the month. That can be also put in a table.<\/p>\n<table class=\"simple\">\n<tr>\n<th>Day of month<\/th>\n<th>Day of non-leap year<\/th>\n<th>Day of leap year<\/th>\n<\/tr>\n<tr>\n<td>January 1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>February 1<\/td>\n<td>32<\/td>\n<td>32<\/td>\n<\/tr>\n<tr>\n<td>March 1<\/td>\n<td>60<\/td>\n<td>61<\/td>\n<\/tr>\n<tr>\n<td>April 1<\/td>\n<td>91<\/td>\n<td>92<\/td>\n<\/tr>\n<tr>\n<td>May 1<\/td>\n<td>121<\/td>\n<td>122<\/td>\n<\/tr>\n<tr>\n<td>June 1<\/td>\n<td>152<\/td>\n<td>153<\/td>\n<\/tr>\n<tr>\n<td>July 1<\/td>\n<td>182<\/td>\n<td>183<\/td>\n<\/tr>\n<tr>\n<td>August 1<\/td>\n<td>213<\/td>\n<td>214<\/td>\n<\/tr>\n<tr>\n<td>September 1<\/td>\n<td>244<\/td>\n<td>245<\/td>\n<\/tr>\n<tr>\n<td>October 1<\/td>\n<td>274<\/td>\n<td>275<\/td>\n<\/tr>\n<tr>\n<td>November 1<\/td>\n<td>305<\/td>\n<td>306<\/td>\n<\/tr>\n<tr>\n<td>December 1<\/td>\n<td>335<\/td>\n<td>336<\/td>\n<\/tr>\n<\/table>\n<p>So we can compute the day of the year as:<\/p>\n<pre>\r\nday_of_year = day_of_year_of_first_month_day + day_of_month - 1\r\n<\/pre>\n<p>We can simplify that a bit by subtracking 1 from the day of the year values in the table above, such that January 1st is the day 0, February 1st is day 31, etc.<\/p>\n<p>The following code sample shows how this can be written in C++:<\/p>\n<pre class=\"lang:c++ decode:true \" >namespace datetools\r\n{\r\n   namespace details\r\n   {\r\n      constexpr unsigned int days_to_month[2][12] =\r\n      {\r\n         \/\/ non-leap year\r\n         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },\r\n         \/\/ leap year\r\n         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 },\r\n      };\r\n   }\r\n\r\n   constexpr bool is_leap(int const year) noexcept\r\n   {\r\n      return year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0);\r\n   }\r\n\r\n   constexpr unsigned int day_of_year(int const year, unsigned int const month, unsigned int const day)\r\n   {\r\n      return details::days_to_month[is_leap(year)][month - 1] + day;\r\n   }\r\n}<\/pre>\n<p>And how it can be used:<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nauto doy1 = datetools::day_of_year(2017, 1, 1);   \/\/ doy1 = 1\r\nauto doy2 = datetools::day_of_year(2017, 8, 3);   \/\/ doy2 = 215\r\nauto doy3 = datetools::day_of_year(2017, 12, 31); \/\/ doy3 = 365\r\nauto doy4 = datetools::day_of_year(2020, 12, 31); \/\/ doy4 = 366\r\n<\/pre>\n<p>This <tt>day_of_year()<\/tt> function can be used with the <tt>date<\/tt> library too. I&#8217;ll just add one more utility function that takes a <tt>date::year_month_day<\/tt> value and returns the day of the year.<\/p>\n<pre class=\"lang:c++ decode:true \" >#include \"date.h\"\r\n\r\nunsigned int day_of_year(date::year_month_day const &amp; dt)\r\n{\r\n   return datetools::day_of_year(\r\n      static_cast&lt;int&gt;(dt.year()),\r\n      static_cast&lt;unsigned&gt;(dt.month()),\r\n      static_cast&lt;unsigned&gt;(dt.day()));\r\n}\r\n\r\nint main()\r\n{\r\n   using namespace date::literals;\r\n\r\n   auto doy1 = day_of_year(2017_y \/ 1 \/ 1);   \/\/ doy1 = 1\r\n   auto doy2 = day_of_year(2017_y \/ 8 \/ 3);   \/\/ doy2 = 215\r\n   auto doy3 = day_of_year(2017_y \/ 12 \/ 31); \/\/ doy3 = 365\r\n   auto doy4 = day_of_year(2020_y \/ dec \/ 31);\/\/ doy4 = 366\r\n}<\/pre>\n<p>And we want to know what day of the year today is then we can do that too:<\/p>\n<pre class=\"lang:c++ decode:true \" >date::year_month_day today = \r\n   date::floor&lt;date::days&gt;(std::chrono::system_clock::now());\r\nauto dayt = day_of_year(today);<\/pre>\n<p>The <tt>day_of_year()<\/tt> function is very simple and does not do any argument checks. That makes it possible to compute dates such as 2017.08.55 or 2017.55.100. Obviously, these not only that do not make sense, but indexing the <tt>days_to_month<\/tt> array beyond its bounds is undefined behavior. That means that in practice you should write a function that validates the arguments and throws an exception upon error. However, in this case, the <tt>day_of_year()<\/tt> can not be <tt>constexpr<\/tt> anymore.<\/p>\n<pre class=\"lang:c++ decode:true \" >namespace datetools\r\n{\r\n   constexpr bool is_leap(int const year) noexcept\r\n   {\r\n      return year % 4 == 0 &amp;&amp; (year % 100 != 0 || year % 400 == 0);\r\n   }\r\n\r\n   unsigned int day_of_year(int const year, unsigned int const month, unsigned int const day)\r\n   {\r\n      static const unsigned int days_to_month[2][12] =\r\n      {\r\n         \/\/ non-leap year\r\n         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },\r\n         \/\/ leap year\r\n         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 },\r\n      };\r\n\r\n      if (month == 0 || month &gt; 12) throw std::out_of_range(\"invalid month\");\r\n      if (day == 0 || day &gt; 31) throw std::out_of_range(\"invalid day\");\r\n\r\n      return days_to_month[is_leap(year)][month - 1] + day;\r\n   }\r\n}<\/pre>\n<p>This would throw an exception on dates like 2017.13.1 or 2017.1.50, but would not do so for 2017.2.30 or 2017.11.31 that are also invalid dates. That can be further corrected by verifying that the day of the month does not exceed the number of days that month can have in the given year.<\/p>\n<pre class=\"lang:c++ decode:true \" >\r\nunsigned int day_of_year(int const year, unsigned int const month, unsigned int const day)\r\n{\r\n   static const unsigned int days_to_month[2][12] =\r\n   {\r\n      \/\/ non-leap year\r\n      { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },\r\n      \/\/ leap year\r\n      { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 },\r\n   };\r\n\r\n   static const unsigned int days_of_month[2][12] = \r\n   {\r\n      \/\/ non-leap year\r\n      { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r\n      \/\/ leap-year\r\n      { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },\r\n   };\r\n\r\n   if (month == 0 || month &gt; 12) throw std::out_of_range(\"invalid month\");\r\n\r\n   auto leap = is_leap(year);\r\n   if (day == 0 || day &gt; days_of_month[leap][month-1]) throw std::out_of_range(\"invalid day\");\r\n\r\n   return days_to_month[leap][month - 1] + day;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have been recently asked on my post on the date library if the library has a function for computing the day of the year. It actually does not, although it is fairly simple to compute it. UPDATE: Howard Hinnant has shown in a comment below how to write a day_of_year() function using the date &#8230; <a title=\"Computing day of year in C++\" class=\"read-more\" href=\"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/\" aria-label=\"Read more about Computing day of year in C++\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[451,488,489,138],"class_list":["post-2532","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-chrono","tag-date","tag-library"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.3 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn how to compute in C++ the day of the year for a given date (with year, month, day).\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,date,chrono,library\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Computing day of year in C++\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn how to compute in C++ the day of the year for a given date (with year, month, day).\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2017-08-03T08:01:31+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2017-08-04T07:24:23+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Computing day of year in C++\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn how to compute in C++ the day of the year for a given date (with year, month, day).\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#article\",\"name\":\"Computing day of year in C++\",\"headline\":\"Computing day of year in C++\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2017-08-03T10:01:31+02:00\",\"dateModified\":\"2017-08-04T09:24:23+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":3,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#webpage\"},\"articleSection\":\"C++, C++, chrono, date, library\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#listItem\",\"name\":\"Computing day of year in C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#listItem\",\"position\":5,\"name\":\"Computing day of year in C++\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/\",\"name\":\"Computing day of year in C++\",\"description\":\"Learn how to compute in C++ the day of the year for a given date (with year, month, day).\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2017\\\/08\\\/03\\\/computing-day-of-year-in-c\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2017-08-03T10:01:31+02:00\",\"dateModified\":\"2017-08-04T09:24:23+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Computing day of year in C++","description":"Learn how to compute in C++ the day of the year for a given date (with year, month, day).","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/","robots":"max-image-preview:large","keywords":"c++,date,chrono,library","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#article","name":"Computing day of year in C++","headline":"Computing day of year in C++","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2017-08-03T10:01:31+02:00","dateModified":"2017-08-04T09:24:23+02:00","inLanguage":"en-US","commentCount":3,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#webpage"},"articleSection":"C++, C++, chrono, date, library"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#listItem","name":"Computing day of year in C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#listItem","position":5,"name":"Computing day of year in C++","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/","name":"Computing day of year in C++","description":"Learn how to compute in C++ the day of the year for a given date (with year, month, day).","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2017-08-03T10:01:31+02:00","dateModified":"2017-08-04T09:24:23+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Computing day of year in C++","og:description":"Learn how to compute in C++ the day of the year for a given date (with year, month, day).","og:url":"https:\/\/mariusbancila.ro\/blog\/2017\/08\/03\/computing-day-of-year-in-c\/","article:published_time":"2017-08-03T08:01:31+00:00","article:modified_time":"2017-08-04T07:24:23+00:00","twitter:card":"summary","twitter:title":"Computing day of year in C++","twitter:description":"Learn how to compute in C++ the day of the year for a given date (with year, month, day)."},"aioseo_meta_data":{"post_id":"2532","title":"Computing day of year in C++","description":"Learn how to compute in C++ the day of the year for a given date (with year, month, day).","keywords":[{"label":"C++","value":"C++"},{"label":"date","value":"date"},{"label":"chrono","value":"chrono"},{"label":"library","value":"library"}],"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:06:01","updated":"2025-12-12 07:46:59","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":3,"uagb_excerpt":"I have been recently asked on my post on the date library if the library has a function for computing the day of the year. It actually does not, although it is fairly simple to compute it. UPDATE: Howard Hinnant has shown in a comment below how to write a day_of_year() function using the date&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">chrono<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">date<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">library<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">chrono<\/span>","<span class=\"advgb-post-tax-term\">date<\/span>","<span class=\"advgb-post-tax-term\">library<\/span>"]}},"comment_count":"3","relative_dates":{"created":"Posted 9 years ago","modified":"Updated 9 years ago"},"absolute_dates":{"created":"Posted on August 3, 2017","modified":"Updated on August 4, 2017"},"absolute_dates_time":{"created":"Posted on August 3, 2017 10:01 am","modified":"Updated on August 4, 2017 9:24 am"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-EQ","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=2532"}],"version-history":[{"count":17,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2532\/revisions"}],"predecessor-version":[{"id":2551,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/2532\/revisions\/2551"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=2532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=2532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=2532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}