Найти количество дней в каждом месяце в данном интервале - C#
Формулировка задачи:
Добрый день. Есть два DateTime. Необходимо найти количество дней в каждом месяце в данном интервале.
Например 12.06.2013 - 12.08.2013 . 18 дней июня, 30 дней июля, 12 дней августа.
Заранее спасибо!
Решение задачи: «Найти количество дней в каждом месяце в данном интервале»
textual
Листинг программы
using System;
using System.Collections.Generic;
namespace ConsoleApplication39
{
class Program
{
static DateTime START = new DateTime(2013, 06, 12);
static DateTime FINISH = new DateTime(2013, 08, 12);
static void Main(string[] args)
{
Console.WriteLine(string.Join("\n", GetDate(START, FINISH)));
Console.ReadKey(true);
}
static string[] GetDate(DateTime start, DateTime finish)
{
List<string> result = new List<string>();
while (true)
{
var current = DateTime.DaysInMonth(start.Year, start.Month);
if (start.AddDays(current - start.Day) > finish)
{
result.Add(string.Format("{0} {1}", start.ToString("MMMM"), finish.Day));
break;
}
else
result.Add(string.Format("{0} {1}", start.ToString("MMMM"), current - start.Day));
start = start.AddDays(current - start.Day + 1);
}
return result.ToArray();
}
}
}