开发者

Date calculations in C#

When given a start date a need to do various calculations on it to produ开发者_JAVA百科ce 3 other dates.

Basically I need to work out what date the user has been billed up to for different frequencies based on the current date.

Bi-Annually (billed twice a year), Quarterly (billed 4 times a year), and Two Monthly (billed ever other month).

Take the date 26/04/2008

- BiAnnually: This date would have been last billed on 26/10/2010 and should give the date 26/04/2011.

- Quarterly: This date would have been last billed on 26/01/2011 and should give the date 26/04/2011.

- Two Month: This date would have been last billed on 26/12/2010 and should give the date 26/02/2011.

Assistance is much appreciated.


I think that you can just do like this:

public void FindNextDate(DateTime startDate, int interval);
  DateTime today = DateTime.Today;
  do {
    startDate = startDate.AddMonths(interval);
  } while (startDate <= today);
  return startDate;
}

Usage:

DateTime startDate = new DateTime(2008, m4, 26);

DateTime bi = FindNextDate(startDate, 6);
DateTime quarterly = FindNextDate(startDate, 3);
DateTime two = FindNextDate(startDate, 2);


I think all you want is something like

DateTime x = YourDateBasis;
y = x.AddMonths(6);
y = x.AddMonths(3);
y = x.AddMonths(2);

Then to edit from comment,

Date Math per the period cycle of the person's account, you would simply need the start and end date and keep adding respective months until you've created all expected months. Almost like that of a loan payment that's due every month for 3 years

DateTime CurrentDate = DateTime.Now;
while( CurrentDate < YourFinalDateInFuture )
{
   CurrentDate = CurrentDate.AddMonths( CycleFrequency );
   Add Record into your table as needed
   Perform other calcs as needed
}


enum BillPeriod
{
   TwoMonth = 2,
   Quarterly = 3,
   SemiAnnually = 6,
   BiAnnually = 24
}

public Pair<Datetime, Datetime> BillDates(Datetime currentBillDate, BillPeriod period)
{
   Datetime LastBill = currentBillDate.AddMonths(-1 * (int)period);
   Datetime NextBill = currentBillDate.AddMonths((int)period);
   return new Pair<Datetime,Datetime>(LastBill, NextBill);
}


This is a terrible solution, but it works. Remember, red-light, green-light, refactor. Here, we're at green-light:

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(GetLastBilled(new DateTime(2008, 4, 26), 6));
            Console.WriteLine(GetNextBilled(new DateTime(2008, 4, 26), 6));

            Console.WriteLine(GetLastBilled(new DateTime(2008, 4, 26), 4));
            Console.WriteLine(GetNextBilled(new DateTime(2008, 4, 26), 4));

            Console.WriteLine(GetLastBilled(new DateTime(2008, 4, 26), 2));
            Console.WriteLine(GetNextBilled(new DateTime(2008, 4, 26), 2));

            Console.WriteLine("Complete...");
            Console.ReadKey(true);
        }

        static DateTime GetLastBilled(DateTime initialDate, int billingInterval) {
            // strip time and handle staggered month-end and 2/29
            var result = initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year);
            while (result > DateTime.Now.Date) {
                result = result.AddMonths(billingInterval * -1);
            }
            return result;
        }

        static DateTime GetNextBilled(DateTime initialDate, int billingInterval) {
            // strip time and handle staggered month-end and 2/29
            var result = initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year);
            while (result > DateTime.Now.Date) {
                result = result.AddMonths(billingInterval * -1);
            }
            result = result.AddMonths(billingInterval);
            return result;
        }
    }
}

This is really tricky. For example, you need to take into account that the date you billed could have been 2/29 on a leap year, and not all months have the same number of days. That's why I did the initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year); call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜