C# 获得两日期之间所有月份(包括跨年)

前台:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        开始<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        结束<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

后台:

protected void Button1_Click(object sender, EventArgs e)
        {
            string Begain_time = TextBox1.Text;
            string End_time = TextBox2.Text;
            string year_b = Begain_time.Substring(0, 4);
            string year_e = End_time.Substring(0, 4);
            string Begain_month = Begain_time.Substring(5, 2);
            string End_month = End_time.Substring(5, 2);
            int year_count=Convert.ToInt32(year_e)-Convert.ToInt32(year_b);

            if (year_count==0)
            {
                int count = Convert.ToInt32(End_month) - Convert.ToInt32(Begain_month);
                for (int i = 0; i <= count; i++)
                {
                    string month = (Convert.ToInt32(Begain_month) + i).ToString();
                    if (month.Length == 1)
                    {
                        month = "0" + month;
                    }
                    DateTime datetime = DateTime.Now;
                    string[] time = datetime.ToString().Split(new char[] { '/' });
                    string date = year_b + "-" + month + "-" + time[2];
                    string first = "";
                    string last = "";
                    if (Convert.ToInt32(month) == Convert.ToInt32(Begain_month))
                    {
                        first = Begain_time;
                    }
                    else
                    {
                        first = Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day).ToString();
                    }
                    if (Convert.ToInt32(month) == Convert.ToInt32(End_month))
                    {
                        last = End_time;
                    }
                    else
                    {
                        last = Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day).AddMonths(1).AddDays(-1).ToString();
                    }
                    if (first.IndexOf(" ") > 0)
                    {
                        first = first.Remove(first.IndexOf(" "));
                    }
                    if (last.IndexOf(" ") > 0)
                    {
                        last = last.Remove(last.IndexOf(" "));
                    }
                    Response.Write(first + "" + last + "");
                }
            }
            else
            {
                int a = Convert.ToInt32(End_month);
                int b = Convert.ToInt32(Begain_month);
                int count = Convert.ToInt32(End_month)+ 12 * year_count - Convert.ToInt32(Begain_month) ;
                for (int i = 0; i <= count; i++)
                {
                    DateTime datetime = DateTime.Now;
                    string[] time = datetime.ToString().Split(new char[] { '/' });
                    string month = "";
                    string date = "";
                    string year = "";
                    int  month_ = Convert.ToInt32(Begain_month) + i;
                    int year_add=0;
                    if (month_>12)
                    {
                        year_add = month_ / 12;
                    }
                    year = (Convert.ToInt32(year_b) + year_add).ToString();
                    if (month_%12==0)
                    {
                        month = "12";
                        if (year_add>0)
                        {
                            year = (Convert.ToInt32(year) - 1).ToString();
                        }
                    }
                    else
                    {
                        month = (month_ - 12 * year_add).ToString();
                    }
                    if (month.Length == 1)
                    {
                        month = "0" + month;
                    }
                    date = year + "-" + month + "-" + time[2];
                    
                    
                    
                    string first = "";
                    string last = "";
                    if (Convert.ToInt32(month) == Convert.ToInt32(Begain_month) && Convert.ToInt32(year) == Convert.ToInt32(year_b))
                    {
                        first = Begain_time;
                    }
                    else
                    {
                        first = Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day).ToString();
                    }
                    if (Convert.ToInt32(month) == Convert.ToInt32(End_month) && Convert.ToInt32(year) == Convert.ToInt32(year_e))
                    {
                        last = End_time;
                    }
                    else
                    {
                        last = Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day).AddMonths(1).AddDays(-1).ToString();
                    }
                    if (first.IndexOf(" ") > 0)
                    {
                        first = first.Remove(first.IndexOf(" "));
                    }
                    if (last.IndexOf(" ") > 0)
                    {
                        last = last.Remove(last.IndexOf(" "));
                    }
                    Response.Write(first+"-"+last+"\t");
                }
            }
        }

 注:输入日期月和日必需是两位,如2015-08-05或2015/08/05

posted @ 2015-08-27 09:39  LovingNow  阅读(2033)  评论(0编辑  收藏  举报
/* 看板娘 */