根據年月以及月中周次,擷取該周開始,結束日期,年月月中

來源:互聯網
上載者:User

根據年月以及月中周次,擷取該周開始,結束日期,年月月中

注意,是該月中的周次,一周最多有6周。

 

        <span style="background-color: rgb(102, 51, 255);">//擷取周開始日期和結束日期(周日-周六)        public string GetWeekDate(int yearly, int monthly, string weekly)        {            string dayStr="";            try            {                DateTime day1 = DateTime.Parse(yearly.ToString() + "-" + monthly.ToString() + "-01");//該月第一天日期                int weekOfDay1 = (Int32)day1.DayOfWeek;//該月第一天是周幾                int dayCount = DateTime.DaysInMonth(yearly, monthly);//該月天數                int endDay = 0;//結束日                DateTime dateEnd = DateTime.Now;//周結束日期,初始化為目前時間                //7*周次 - 第一周中上月天數 > 本月天數                if (7 * Int32.Parse(weekly) - weekOfDay1 > dayCount)                {                    endDay = 7 * Int32.Parse(weekly) - weekOfDay1 - dayCount;                    DateTime dateLast = DateTime.Parse(yearly.ToString() + "-" + monthly.ToString() + "-" + dayCount.ToString());//該月最後一天日期                    dateEnd = dateLast.AddDays(endDay);                }                //7*周次 - 第一周中上月天數 > 本月天數                if (7 * Int32.Parse(weekly) - weekOfDay1 <= dayCount)                {                    endDay = 7 * Int32.Parse(weekly) - weekOfDay1;                    dateEnd = DateTime.Parse(yearly.ToString() + "-" + monthly.ToString() + "-" + endDay.ToString());                }                DateTime dateStart = dateEnd.AddDays(-6); //周開始日期                dayStr = dateStart.ToString("yyyy-MM-dd") +" ~ " + dateEnd.ToString("yyyy-MM-dd");            }            catch (Exception)            {                throw;            }            return dayStr;        }</span>


調用:var resultWeekDay = GetWeekDate(2015, 4, 5);//4月第5周

結果:2015-04-26  ~ 2015-05-02

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.