C# 根据第几周和季度 获取开始时间和结束时间

 /// <summary>
        /// 根据第几周 获取开始时间和结束时间
        /// </summary>
        /// <param name="week">周数</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        private Tuple<DateTime, DateTime> GetWeeks(int? week, int? month)
        {

            DateTime dt = DateTime.Parse($"{DateTime.Now.Year}-{month.Value}-{DateTime.Now.Day}");
            string[] arrDate0 = new string[6];
            string[] arrDate1 = new string[6];
            //年
            int year = dt.Year;
            //月 
            //当前月第一天
            DateTime weekStart = new DateTime(year, month.Value, 1);
            //该月的最后一天
            DateTime monEnd = weekStart.AddMonths(1).AddDays(-1);
            int i = 1;
            //当前月第一天是星期几
            int dayOfWeek = Convert.ToInt32(weekStart.DayOfWeek.ToString("d"));
            //该月第一周结束日期
            DateTime weekEnd = dayOfWeek == 0 ? weekStart : weekStart.AddDays(7 - dayOfWeek);
            string content = "";
            // content += "第" + i + "周起始日期: " + weekStart.ToShortDateString() + "   结束日期: " + weekEnd.ToShortDateString() + "\n";
            arrDate0[0] = weekStart.ToString("yyyy-MM-dd 00:00:00");
            arrDate1[0] = weekEnd.ToString("yyyy-MM-dd 23:59:59");

            //当日期小于或等于该月的最后一天

            while (weekEnd.AddDays(1) <= monEnd)
            {
                i++;
                //该周的开始时间
                weekStart = weekEnd.AddDays(1);
                //该周结束时间
                weekEnd = weekEnd.AddDays(7) > monEnd ? monEnd : weekEnd.AddDays(7);

                arrDate0[i - 1] = weekStart.ToString("yyyy-MM-dd 00:00:00");
                arrDate1[i - 1] = weekEnd.ToString("yyyy-MM-dd 23:59:59");
                // content += "第" + i + "周起始日期: " + weekStart.ToShortDateString() + "   结束日期: " + weekEnd.ToShortDateString() + "\n";
            }

            content += year + "年" + month + "月共有" + i + "周\n";
            return Tuple.Create(DateTime.Parse(arrDate0[week.Value]), DateTime.Parse(arrDate1[week.Value]));
        }

        /// <summary>
        /// 根据季度 获取开始时间和结束时间
        /// </summary>
        /// <param name="quarter">季度</param>
        /// <returns></returns>
        private Tuple<DateTime, DateTime> GetQurater(string quarter)
        {
            DateTime dt = new DateTime();
            switch (quarter)
            {
                case "一季度":
                    dt = DateTime.Parse($"{DateTime.Now.Year},1, 01");
                    break;
                case "二季度":
                    dt = DateTime.Parse($"{DateTime.Now.Year},4, 01");
                    break;
                case "三季度":
                    dt = DateTime.Parse($"{DateTime.Now.Year},7, 01");
                    break;
                case "四季度":
                    dt = DateTime.Parse($"{DateTime.Now.Year},10, 01");
                    break;

            }
     
            //本季度初 
            string start = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day).ToString("yyyy-MM-01 00:00:00");  //本季度初    
            //获取本季度的最后一天  
            string end = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day).AddMonths(3).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"); ;  //
          
          

            return Tuple.Create(DateTime.Parse(start), DateTime.Parse(end));
        }

  

posted @ 2017-07-27 11:09  骚年丶勿忘初心。  阅读(3036)  评论(0编辑  收藏  举报