Date calculation in C #

When assigning a start date, you must perform various calculations to make 3 more dates.

Basically, I need to find out on what date the user billed for different frequencies depending on the current date.

Bi-Annually (billed twice a year), Quarterly (billed 4 times a year), and two months (billed for any other month).

Take part 04/04/2008
- BiAnnually: this date would be the last set on 10/26/2010 and should indicate the date 04/26/2011.
- Quarterly: this date would be the last set on 01/26/2011 and should indicate the date 04/26/2011.
- Two months: this date would be the last set on 12/26/2010 and should indicate the date 02/26/2011.

Help is much appreciated.

+4
source share
4 answers

I think you can just like that:

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

Using:

 DateTime startDate = new DateTime(2008, m4, 26); DateTime bi = FindNextDate(startDate, 6); DateTime quarterly = FindNextDate(startDate, 3); DateTime two = FindNextDate(startDate, 2); 
+3
source

I think all you need is something like

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

Then, to edit the comment,

The math date for the cycle of a person’s account period, you just need a start and end date and continue to add the appropriate months until you have created all the expected months. Almost the same as paying a loan, which is 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 } 
0
source
 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); } 
0
source

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) { // 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 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); .

0
source

Source: https://habr.com/ru/post/1336480/


All Articles