using System.Collections.Generic;
public static void Main()
var billingPeriodStart = new DateTime(billingYear, billingMonth, 1);
var billingPeriodEnd = billingPeriodStart.AddMonths(1);
ActiveFrom = new DateTime(2018, 04, 05),
ActiveTo = (DateTime?)new DateTime(2032, 12, 31)
if (endpointObject.ActiveFrom > billingPeriodEnd)
if (endpointObject.ActiveTo < billingPeriodStart)
var activePeriodStart = billingPeriodStart;
if (endpointObject.ActiveFrom > billingPeriodStart)
activePeriodStart = endpointObject.ActiveFrom;
var activePeriodEnd = billingPeriodEnd;
if (endpointObject.ActiveTo < activePeriodEnd)
activePeriodEnd = endpointObject.ActiveTo.Value.AddDays(1);
var daysActive = (activePeriodEnd - activePeriodStart).TotalDays;
var daysInMonth = DateTime.DaysInMonth(billingYear, billingMonth);
var fractionOfMonth = (decimal)daysActive / daysInMonth;
Console.WriteLine($"Active from: {activePeriodStart}");
Console.WriteLine($"Active to: {activePeriodEnd}");
Console.WriteLine($"{daysActive} of {daysInMonth} days active = {fractionOfMonth:0.0000} = {(fractionOfMonth * 100m):0.00}%");