Skip to content

Instantly share code, notes, and snippets.

@mrshridhara
Created July 9, 2014 07:04
Show Gist options
  • Save mrshridhara/c4f66f9ce866ad4c4660 to your computer and use it in GitHub Desktop.
Save mrshridhara/c4f66f9ce866ad4c4660 to your computer and use it in GitHub Desktop.
Splits a date range of 1 year on a monthly basis.
public static class Program
{
public static void Main(string[] arga)
{
const int TOTAL_MONTHS = 12;
var fromDate = new DateTime(2013, 08, 15);
var toDate = new DateTime(2014, 08, 14);
var totalDays = 0;
var monthlyEndDate = toDate;
for (var monthCount = 1; monthCount <= TOTAL_MONTHS; monthCount++)
{
var monthlyStartDate = fromDate.AddMonths(TOTAL_MONTHS - monthCount);
var dayCount = (monthlyEndDate.Subtract(monthlyStartDate).Days + 1);
Console.WriteLine
(
monthCount.ToString("00") + ") " +
"From: " + monthlyStartDate.ToString("dd-MMM-yyyy") + " to " + monthlyEndDate.ToString("dd-MMM-yyyy") +
" => " + dayCount + " days."
);
totalDays += dayCount;
monthlyEndDate = monthlyStartDate.AddDays(-1);
}
Console.WriteLine();
Console.WriteLine("Total days: " + totalDays);
Console.Read();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment