C# 計算月之間的月份集合

來源:互聯網
上載者:User

public static string StructSql(int startYear, int startMonth, int endYear, int endMonth)
        {
            StringBuilder tempSql = new StringBuilder();
            DateTime dtStart = new DateTime(startYear, startMonth, 1);
            DateTime dtEnd = new DateTime(endYear, endMonth, 1);
            TimeSpan ts = dtEnd - dtStart;
            int tempStartYear = startYear;
            int tempStartMonth = startMonth;
            if (ts.TotalDays > 25)
            {

                for (int i = startMonth; i <= 12; i++)
                {
                    if (i == startMonth && tempStartYear == startYear)
                        tempSql.AppendFormat("select '{0}' as A,'{1}' as B from dual\n", tempStartYear.ToString(), tempStartMonth.ToString());
                    else
                        tempSql.AppendFormat(" union all select '{0}' as A,'{1}' as B from dual\n", tempStartYear.ToString(), tempStartMonth.ToString());

                 
                    if (tempStartYear >= endYear && tempStartMonth >= endMonth)
                        break;

                    if (i == 12)
                    {
                        tempStartYear++;
                        i = 0;
                        tempStartMonth = 0;

                    }

                    tempStartMonth++;
                 
                }
            }
            else
            {
               // Page.RegisterStartupScript("message", "<script type='text/javascript'>alert('查詢的截至時間小於一個月不能查詢!')</script>");
                return "";
                //Response.End();
            }
            return tempSql.ToString();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.