This is a terrible decision, but it works. Remember, red light, green light, refactoring. Here we are in 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) {
This is really hard. For example, you need to consider that the date you billed may be 2/29 in a leap year, and not all months have the same number of days. That is why I made an initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year); call initialDate.Date.AddYears(DateTime.Now.Year - initialDate.Year); .
source share