During one project development there is one scenario which made me write this post.
Scenario is as follows
Company has clients, each has different invoice start date and invoice frequency. I had to calculate and display next invoice date from current date + next month. i.e. invoice will always be raised in next month of service completion. e.g. Client A has invoice start date 01-Jan-2021 and invoice frequency is 5 months. Date when I am writing this post is 17-April-2021. So now next invoice date for Client A is 01-June-2021. For client B invoice start date is 01-March-2021 and invoice frequency is 3 months, then its next invoice date is 01-June-2021 and so on for all other client.
To handle this scenarios I have written following function in c# which will always returns next invoice date from Current date.
public class DateHelper
{
public static DateTime nextInvoiceDate(DateTime? InvoicingStartDate,
string invoicingFrequency)
{
int occurrence =
(DateTime.Now.Month - Convert.ToInt32(InvoicingStartDate?.Month))
/ Convert.ToInt32(invoicingFrequency);
int restInt =
(DateTime.Now.Month - Convert.ToInt32(InvoicingStartDate?.Month))
% Convert.ToInt32(invoicingFrequency);
DateTime next = InvoicingStartDate.Value;
int multi;
if (restInt == 0 && DateTime.Now < InvoicingStartDate)
{
if (Convert.ToInt32(invoicingFrequency) == 1 && occurrence < 0)
{
multi = 1;
}
else
{
multi = Convert.ToInt32(invoicingFrequency)
* Math.Abs(occurrence);
}
}
else
{
if (Convert.ToInt32(invoicingFrequency) == 1)
{
multi = Convert.ToInt32(invoicingFrequency)
* (Math.Abs(occurrence));
}
else
{
multi = Convert.ToInt32(invoicingFrequency)
* (Math.Abs(occurrence) + 1);
}
}
return next.AddMonths(multi);
}
}
- How to calculate a future date based on a given date ?
- Calculate next due date based on start date frequency and current date ?
- How to nearest future date from given date and frequency ?
- Best way to calculate next date from start plus frequency
- How to calculate next meeting date based on given frequency ?