1

Any help would be appreciated. I have a company that calculates interest where if they have a property for one day in the month of June by July 1st even though it's only been 2 days they get the interest for 2 months. I am having a little trouble figuring out how to do this. Here is what I have which works in some cases but in the case like I mentioned above is there any way for me to figure out month calculation so that if the buy date is 1/29/2018 and it's 3/1/2018 instead of 2 months like mine would calculate it would actually be 3 months interest?

string startDate = "1/15/2018";
    DateTime start = Convert.ToDateTime(startDate);
    DateTime now = DateTime.Now;
    double numberOfDays = (now.Date - start.Date).TotalDays / (365.25 / 12);
    double rounded = Math.Round(numberOfDays, 0);
    monthsLabel.Text = rounded.ToString();
  • 1
    Don't you have a job that just runs on the first of the month to calculate the interest for the previous month? How are you handling compound interest? At any rate, you'll probably have better luck on the math forum since this isn't really a programming question. – itsme86 Mar 21 '18 at 16:23
  • 1
    int months = (now.Month - start.Month) + 1 – Simply Ged Mar 21 '18 at 16:28
  • @SimplyGed : what if start date was last year? (3 - 12) + 1 = -8 – devlin carnate Mar 21 '18 at 16:29
  • I am planning to set up a job on the 1st of the month. Just have to figure out how to program it! @SimplyGed exactly... they can gain interest for up to 36 months from the start date – ksuProgrammer Mar 21 '18 at 16:30
  • @SimplyGed I tried that method however that would only work for one calendar year I believe. – ksuProgrammer Mar 21 '18 at 16:30
  • @devlincarnate Then I guess you would probably need to add the difference in years * 12 months: `int months = (now.Month - start.Month) + 1 + (now.Year - start.Year) * 12;` – Rufus L Mar 21 '18 at 16:40
  • @RufusL you got it! Much appreciated! – ksuProgrammer Mar 21 '18 at 16:42

0 Answers0