Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could take off the ticks since the start of time (well not the start of time, but 01/01/0001) :</p> <pre><code>// Assuming date 1 is the later date DateTime newDate = new DateTime(myDate1.Ticks - myDate2.Ticks); // NOTE : you might need to take 1 off each property, as they start at 1 string.Format("{0} Years, {1} Months, {2} Days", newDate.Year, newDate.Month, newDate.Day); </code></pre> <p><strong>or</strong></p> <p>You could create a module that will do your workings for you that will loop counting the number of years between the dates until either the years are equal or the year is one less in the case that the earlier month is greater. Then you can loop until the months are equal without forgetting to drop from 12 to 1.</p> <pre><code>int yearCount = 0; int monthCount = 0; int earliestYear = 0; int earliestMonth = 0; int latestYear = 0; int latestMonth = 0; // Get the earlier date, assuming that you haven't calculated which date is the latter already. if (myDate1 &gt; myDate2) { earliestYear = myDate2.Year; earliestMonth = myDate2.Month; } else { latestYear = myDate1.Year; latestMonth = myDate1.Month; } // Get the years between (remember not to include a year where the earlier dates month is greater than the latter. E.g. 09/2011 -&gt; 01/2013 will only be 1 year, not 2! while(earliestYear &lt; latestYear &amp;&amp; (earliestMonth &lt;= latestMonth || earliestYear &lt; (latestYear - 1))) { yearCount++; earliestYear++; } // Finally get the months between, moving back to january after december. while (earliestMonth != latestMonth) { monthCount++; if (earliestMonth == 12) { earliestMonth = 1; } else { earliestMonth++; } } string.Format("{0} years and {1} months", yearCount, monthCount); </code></pre> <p><em>This is untested code written on the fly to give you a rough idea.</em></p> <p><em>This also assumes that you would represent there being 1 month between two dates such as 29/02/1988 and 01/03/1988 (British Date Time Format)</em></p> <p><strong>or</strong> </p> <p>You could try using a timespan to complete the task, there is no way to get the years without a bit of manual code although I'm sure you can figure that part out :-) (e.g. something like <code>days / 365.25</code>)</p> <pre><code>TimeSpan timebetween = myDate1 - myDate2; </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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