C# Calculate the next end date of a subscription

Copper Contributor

I am trying to calculate the next date on which a subscription will end, for example if it started on 03-15-2023 and you cancel it on 09-25-2023, the date that should give results is 10-15-2023 which is until when it will be valid.

how can I get the new date

1 Reply

Something like the below should work. The AddMonths method handles roll-over to the next month (and year if necessary).

 

DateOnly newExpirationDate = new DateOnly(cancelDate.Year, cancelDate.Month, startDate.Day);
if (cancelDate.Day > startDate.Day) {
newExpirationDate = newExpirationDate.AddMonths(1);
}