Note that there are some explanatory texts on larger screens.

plurals
  1. POThe implementation of calculating the number of days between 2 dates
    primarykey
    data
    text
    <p>What's the most efficient way to calculate the number of days between 2 dates? Basically I'm asking how our favourate datetime libraries are implemented.</p> <p>I quickly implemented a solution that is ~O(n) as I run through 1 iteration per 4 years. (Code attached below)</p> <p>I was asked by an intro to problem solving with computers class to implement this, but they're simply iterating through <em>everyday</em> instead of every 4 years.. so I'm not content with that solution and came up with the one below. However, is there a more efficient solution available? If so, how do they accomplish it?</p> <pre><code>#include &lt;iostream&gt; using namespace std; #define check_leap(year) ((year % 400 == 0) || ((year % 4 == 0) &amp;&amp; (year % 100 != 0))) #define debug(n) cout &lt;&lt; n &lt;&lt; endl int get_days(int month, bool leap){ if (month == 2){ if (leap) return 29; return 28; } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){ return 31; } else { return 30; } } int days[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; #define days_prior_to_month(n) days[n-2] int num_days_between(int month1, int day1, int month2, int day2, bool leap){ if (month2 &gt; month1) return ((days_prior_to_month(month2) - days_prior_to_month(month1+1)) + get_days(month1, leap) - day1 + 1 + day2) + ((leap &amp;&amp; month1 &lt;= 2 &amp;&amp; 2 &lt;= month2) ? 1 : 0); else if (month2 == month1) return day2; return -1; } int main(int argc, char * argv[]){ int year, month, day, year2, month2, day2; cout &lt;&lt; "Year: "; cin &gt;&gt; year; cout &lt;&lt; "Month: "; cin &gt;&gt; month; cout &lt;&lt; "Day: "; cin &gt;&gt; day; cout &lt;&lt; "Year 2: "; cin &gt;&gt; year2; cout &lt;&lt; "Month 2: "; cin &gt;&gt; month2; cout &lt;&lt; "Day 2: "; cin &gt;&gt; day2; int total = 0; if (year2 != year){ int leapyears = 0; total += num_days_between(month, day, 12, 31, check_leap(year)); debug(total); total += num_days_between(1, 1, month2, day2, check_leap(year2)); debug(total); int originalyear = year; year++; year = year + year % 4; while (year &lt;= year2-1){ leapyears += check_leap(year) ? 1 : 0; year += 4; } total += leapyears * 366; debug(total); total += max(year2 - originalyear - leapyears - 1, 0) * 365; debug(total); } else { total = num_days_between(month, day, month2, day2, check_leap(year)); } cout &lt;&lt; "Total Number of Days In Between: " &lt;&lt; total &lt;&lt; endl; system("PAUSE"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload