Qui andiamo
public static IEnumerable<Tuple<string, int>> MonthsBetween(
DateTime startDate,
DateTime endDate)
{
DateTime iterator;
DateTime limit;
if (endDate > startDate)
{
iterator = new DateTime(startDate.Year, startDate.Month, 1);
limit = endDate;
}
else
{
iterator = new DateTime(endDate.Year, endDate.Month, 1);
limit = startDate;
}
var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
while (iterator <= limit)
{
yield return Tuple.Create(
dateTimeFormat.GetMonthName(iterator.Month),
iterator.Year);
iterator = iterator.AddMonths(1);
}
}
E ovviamente chiamare in questo modo
var startDate = DateTime.ParseExact("01/01/2011", "MM/dd/yyyy");
var endDate = DateTime.ParseExact("11/30/2011", "MM/dd/yyyy");
var months = MonthsBetween(startDate, endDate);
I risultati dovrebbero essere qualcosa di simile
{
{ "January", 2011 },
{ "February", 2011 },
{ "March", 2011 },
{ "April", 2011 },
{ "May", 2011 },
{ "June", 2011 },
{ "July", 2011 },
{ "August", 2011 },
{ "September", 2011 },
{ "October", 2011 },
{ "November", 2011 },
}
i nomi dei mesi di essere dipendente dalla vostra cultura che , Penso, è esattamente quello che hai chiesto, giusto?
Sembra una domanda per i compiti. L'idea è che tu lavori questo per te stesso. Se hai problemi specifici con la soluzione che hai mai trovato, sarò lieto di aiutarti. –
Duplicato: http://stackoverflow.com/questions/1525990/difference-in-months – h1ghfive
@ h1ghfive - no, non è un duplicato di quello – PHeiberg