我尝试弹出一个显示给定日期的月份和年份的 msgbox,例如我的输入是:
2012 年 7 月和 2013 年 2 月
输出应该是:
7/2012,8/2012,9/2012,10/2012,11/2012,12/2012,1/2013,2/2013
我写:
string datePart1;
string datePart2;
string[] date1 = new string[] { "" };
string[] date2 = new string[] { "" };
private void button1_Click(object sender, EventArgs e)
{
DateTime endDate = new DateTime(2013, 2, 1); // i will be having the date time as a variable from a textbox
DateTime begDate = new DateTime(2012, 7, 1); // i will be having the date time as a variable from a text box
int year, month;
if (endDate.Month - begDate.Month < 0)
{
month = (endDate.Month - begDate.Month) + 12;
endDate = new DateTime(endDate.Year - 1, endDate.Month, endDate.Day);
}
else
month = endDate.Month - begDate.Month;
year = endDate.Year - begDate.Year;
上面的代码计算了时间差,但我的输出尝试没有奏效。