zoukankan      html  css  js  c++  java
  • 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+"	");
                    }
                }
            }

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

  • 相关阅读:
    了解PCI Express的Posted传输与Non-Posted传输
    最强加密算法?AES加解密算法Matlab和Verilog实现
    校招必看硬核干货:IC前端这样学,秒变offer收割机!
    一次压力测试Bug排查-epoll使用避坑指南
    硬核干货 | C++后台开发学习路线
    Web服务器项目详解
    O准备如何苟进复赛圈?华为软挑开挂指南(附赛题预测)
    Linux最大文件句柄(文件描述符)限制和修改
    linux中对EINTR错误的处理
    C/C++实现单向循环链表(尾指针,带头尾节点)
  • 原文地址:https://www.cnblogs.com/Tinoloving/p/4762364.html
Copyright © 2011-2022 走看看