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

  • 相关阅读:
    创建Variant数组
    ASP与存储过程(Stored Procedures)
    FileSystemObject对象成员概要
    Kotlin 朱涛9 委托 代理 懒加载 Delegate
    Kotlin 朱涛 思维4 空安全思维 平台类型 非空断言
    Kotlin 朱涛7 高阶函数 函数类型 Lambda SAM
    Kotlin 朱涛16 协程 生命周期 Job 结构化并发
    Proxy 代理模式 动态代理 cglib MD
    RxJava 设计理念 观察者模式 Observable lambdas MD
    动态图片 Movie androidgifdrawable GifView
  • 原文地址:https://www.cnblogs.com/Tinoloving/p/4762364.html
Copyright © 2011-2022 走看看