如果Asp.net:
DateTime dt1 = new DateTime();
dt1 = DateTime.ParseExact("01/04/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture);
DateTime dt2 = new DateTime();
dt2 = DateTime.ParseExact("31/08/2012", "dd/M/yyyy", CultureInfo.InvariantCulture);
while (dt2 > dt1)
{
dt1 = dt1.AddMonths(1);
this.dateDropDownList.Items.Add(dt1.ToString("MM/yyyy", CultureInfo.InvariantCulture));
}
如果Windows窗體:
DateTime dt1 = new DateTime();
dt1 = DateTime.ParseExact("01/04/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture);
DateTime dt2 = new DateTime();
dt2 = DateTime.ParseExact("31/08/2012", "dd/M/yyyy", CultureInfo.InvariantCulture);
while (dt2 > dt1)
{
dt1 = dt1.AddMonths(1);
this.dateComboBox.Items.Add(dt1.ToString("MM/yyyy", CultureInfo.InvariantCulture));
}
來源
2012-07-11 11:53:40
MMK
你嘗試過什麼了嗎?此外,簡單搜索C#和日期時間將爲您提供有關如何從日期時間對象獲取月份和年份的答案。 – 2012-07-11 10:33:11