{"id":7568,"date":"2025-02-23T12:17:52","date_gmt":"2025-02-23T18:17:52","guid":{"rendered":"https:\/\/justinparrtech.com\/JustinParr-Tech\/?p=7568"},"modified":"2025-04-22T19:08:01","modified_gmt":"2025-04-23T00:08:01","slug":"excel-number-of-sundays-a-better-answer","status":"publish","type":"post","link":"https:\/\/justinparrtech.com\/JustinParr-Tech\/excel-number-of-sundays-a-better-answer\/","title":{"rendered":"Excel &#8211; Number of Sundays &#8211; A better Answer"},"content":{"rendered":"<h1>Excel &#8211; Number of Sundays &#8211; A better Answer<\/h1>\n<p>I looked up how to count the number of Sundays between two dates&#8230;. the so-called answer shocked me.\u00a0 There are a couple of popular answers, but they are horribly inefficient.<\/p>\n<p>Let me show you a better way&#8230;..<\/p>\n<p>Update 4\/22\/2025<\/p>\n<p>Due to a severe copy and paste error, the formulas were completely wrong.\u00a0 8-(<\/p>\n<p>These have now been corrected.<\/p>\n<p>&nbsp;<\/p>\n<h2>Solution<\/h2>\n<p>Let&#8217;s say we have the following cells:<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>A<\/td>\n<td>B<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Start<\/td>\n<td>1\/1\/2025<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>End<\/td>\n<td>5\/1\/2025<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Which Day<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We will follow the Excel convention, where Sunday = 1, to find the number of &#8220;1&#8221; days (cell B3) between 1\/1\/2025 (cell B1) and 5\/1\/2025 (cell B2).<\/p>\n<p>Between the two dates:<\/p>\n<pre>=INT((B2-(B1+B3-WEEKDAY(B1)+7))\/7)+1<\/pre>\n<p><strong>Correctly returns 17<\/strong><\/p>\n<p>Between <strong>and including<\/strong> the two dates:<\/p>\n<pre>=INT((B2-(B1+B3-WEEKDAY(B1)+7))\/7)+1+IF(WEEKDAY(B1)=B3,1,0)<\/pre>\n<p>Correctly returns 17.\u00a0 If we were to change B1 to 1\/5 (a Sunday), the result would be 16 without the extra IF term.<\/p>\n<p>&nbsp;<\/p>\n<p>For a more detailed explanation&#8230;<\/p>\n<p><!--more--><\/p>\n<h2>Quick Background on Excel Dates<\/h2>\n<p>Excel stores a date as a &#8220;dateserial&#8221;&#8230; an integer number of days since 1\/1\/1900.<\/p>\n<p>So today&#8217;s dateserial (2\/20\/25) is 45,708.<\/p>\n<p>If I add one, 45,708 + 1 = 45,709 = 2\/21\/2025<\/p>\n<p>Time is a decimal value between integers.<\/p>\n<p>For example, 5 PM = 1700 hours.\u00a0 So 2\/20\/2025 + (17\/24) = 45,708.7083<\/p>\n<p>To add days, you add an integer number.\u00a0 2\/20\/2025 + 5 = 2\/25\/2025<\/p>\n<p>To find the number of days between the two, just subtract 2\/25\/2025 &#8211; 2\/20\/2025 = 5.<\/p>\n<p>&nbsp;<\/p>\n<h2>Detailed Explanation<\/h2>\n<p>The first thing we need to do is find the first Sunday within the range.<\/p>\n<p>The WEEKDAY function returns a number 1-7 indicating the day of the week for a given date, where 1=Sunday, etc.<\/p>\n<p>If we subtract B3 (the day we are looking for) from the weekday of the start date, we get the number of days offset from the start date.<\/p>\n<pre>=WEEKDAY(B1)<\/pre>\n<p>Returns &#8220;4&#8221; because 1\/1\/2025 is on a Wednesday<\/p>\n<pre>=B3-WEEKDAY(B1)<\/pre>\n<p>Returns -3, which is an offset. If we go BACK 3 days from 1\/1, that&#8217;s the last Sunday in Dec, 2024.<\/p>\n<pre>=B3-WEEKDAY(B1)+7<\/pre>\n<p>By adding 7, we ensure that we always have a positive offset from the start date (B1).<\/p>\n<pre>=B1+B3-WEEKDAY(B1)+7<\/pre>\n<p>In this case, -3+7 = 4, and sure enough, B1 (start date) is on a Wednesday, if we add 4 days to B1, we get 1\/5, which is in fact the first Sunday on or after B1.<\/p>\n<p>Now that we have identified the correct starting point, we can simply subtract from the end date to get the total number of days.<\/p>\n<pre>=B2-(B1+B3-WEEKDAY(B1)+7)<\/pre>\n<p>Returns 116.\u00a0 Since =B2-B1 returns 120, this checks out&#8230;\u00a0 Remember that our offset to Sunday was 4 days.<\/p>\n<p>Since every Sunday is exactly 7 days from the previous, we can simply divide by 7.<\/p>\n<pre>=INT((B2-(B1+B3-WEEKDAY(B1)+7))\/7)<\/pre>\n<p>Returns 16.\u00a0 We use the INT function to return an integer&#8230;we don&#8217;t care about the fractional number of days between the last Sunday and the end date.<\/p>\n<p>However, we have a problem&#8230; there are\u00a0<strong>17<\/strong> Sundays between the two.\u00a0 Remember that we subtracted (and therefore excluded) the first Sunday&#8230;<\/p>\n<pre>=INT((B2-(B1+B3-WEEKDAY(B1)+7))\/7)+1<\/pre>\n<p>Returns 17.\u00a0 We can simply add 1 to put back the first Sunday that we subtracted.<\/p>\n<p>We can confirm this by changing B2 to 5\/4\/2025 (a Sunday), and the same formula correctly returns <strong>18<\/strong>.<\/p>\n<p><strong>CAVEAT:<\/strong>\u00a0 If our starting date is also Sunday, for example 1\/5, the formula reports that there are only 17 Sundays between 1\/5 and 5\/4.\u00a0 This is technically correct, as now the first Sunday is no longer BETWEEN the two dates.\u00a0 However, this might not be the behavior you expect.\u00a0 If not, just add an IF:<\/p>\n<pre>=INT((B2-(B1+B3-WEEKDAY(B1)+7))\/7)+1+IF(WEEKDAY(B1)=B3,1,0)<\/pre>\n<p>The nice thing about this formula is that we can count any weekday we want.\u00a0 For example, we can <strong>count Fridays by changing B3 to 6<\/strong>.<\/p>\n<p>I hope you find this useful, and happy Excelling!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Excel &#8211; Number of Sundays &#8211; A better Answer I looked up how to count the number of Sundays between two dates&#8230;. the so-called answer shocked me.\u00a0 There are a couple of popular answers, but they are horribly inefficient. Let me show you a better way&#8230;.. Update 4\/22\/2025 Due to a severe copy and paste [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7568","post","type-post","status-publish","format-aside","hentry","category-other-stuff","post_format-post-format-aside"],"_links":{"self":[{"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/posts\/7568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/comments?post=7568"}],"version-history":[{"count":10,"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/posts\/7568\/revisions"}],"predecessor-version":[{"id":7588,"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/posts\/7568\/revisions\/7588"}],"wp:attachment":[{"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/media?parent=7568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/categories?post=7568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justinparrtech.com\/JustinParr-Tech\/wp-json\/wp\/v2\/tags?post=7568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}