C#获取指定月指定周的日期范围 根据当前时间获取本月开始日期和结束日期

  1. /// <summary> 
  2. /// 获取指定月份指定周数的开始日期 
  3. /// </summary> 
  4. /// <param name="year">年份</param> 
  5. /// <param name="month">月份</param> 
  6. /// <param name="index">周数</param> 
  7. /// <returns></returns> 
  8. private DateTime GetStartDayOfWeeks(int year, int month, int index) 
  9.     if (year < 1600 || year > 9999) 
  10.     { 
  11.         MessageBox.Show("年份超限"); 
  12.         return DateTime .MinValue ; 
  13.     } 
  14.     if (month < 0 || month > 12) 
  15.     { 
  16.         MessageBox.Show("月份错误"); 
  17.         return DateTime .MinValue ; 
  18.     } 
  19.     if (index < 1) 
  20.     { 
  21.         MessageBox.Show("周数错误"); 
  22.         return DateTime.MinValue; 
  23.     } 
  24.     DateTime startMonth = new DateTime(year, month, 1);  //该月第一天 
  25.     int dayOfWeek = 7; 
  26.     if (Convert.ToInt32(startMonth.DayOfWeek.ToString("d")) > 0) 
  27.         dayOfWeek = Convert.ToInt32(startMonth.DayOfWeek.ToString("d"));  //该月第一天为星期几 
  28.     DateTime startWeek = startMonth.AddDays(1 - dayOfWeek);  //该月第一周开始日期 
  29.     //DateTime startDayOfWeeks = startWeek.AddDays((index - 1) * 7);  //index周的起始日期 
  30.     DateTime startDayOfWeeks = startWeek.AddDays(index * 7);  //index周的起始日期 
  31.     if ((startDayOfWeeks - startMonth.AddMonths(1)).Days > 0)  //startDayOfWeeks不在该月范围内 
  32.     { 
  33.         MessageBox.Show("输入周数大于本月最大周数"); 
  34.         return DateTime.MinValue; 
  35.     } 
  36.     return startDayOfWeeks; 
        /// <summary>
        /// 获取指定月份指定周数的开始日期
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <param name="index">周数</param>
        /// <returns></returns>
        private DateTime GetStartDayOfWeeks(int year, int month, int index)
        {
            if (year < 1600 || year > 9999)
            {
                MessageBox.Show("年份超限");
                return DateTime .MinValue ;
            }
            if (month < 0 || month > 12)
            {
                MessageBox.Show("月份错误");
                return DateTime .MinValue ;
            }
            if (index < 1)
            {
                MessageBox.Show("周数错误");
                return DateTime.MinValue;
            }
            DateTime startMonth = new DateTime(year, month, 1);  //该月第一天
            int dayOfWeek = 7;
            if (Convert.ToInt32(startMonth.DayOfWeek.ToString("d")) > 0)
                dayOfWeek = Convert.ToInt32(startMonth.DayOfWeek.ToString("d"));  //该月第一天为星期几
            DateTime startWeek = startMonth.AddDays(1 - dayOfWeek);  //该月第一周开始日期
            //DateTime startDayOfWeeks = startWeek.AddDays((index - 1) * 7);  //index周的起始日期
            DateTime startDayOfWeeks = startWeek.AddDays(index * 7);  //index周的起始日期
            if ((startDayOfWeeks - startMonth.AddMonths(1)).Days > 0)  //startDayOfWeeks不在该月范围内
            {
                MessageBox.Show("输入周数大于本月最大周数");
                return DateTime.MinValue;
            }
            return startDayOfWeeks;
        }


 

 

(

  1. //当前时间 
  2. DateTime dt = DateTime.Now; 
  3. //本月第一天时间     
  4. DateTime dt_First = dt.AddDays(1 - (dt.Day)); 
  5. //获得某年某月的天数   
  6. int year = dt.Date.Year; 
  7. int month = dt.Date.Month; 
  8. int dayCount = DateTime.DaysInMonth(year, month); 
  9. //本月最后一天时间   
  10. DateTime dt_Last = dt_First.AddDays(dayCount - 1); 
            //当前时间
            DateTime dt = DateTime.Now;
            //本月第一天时间    
            DateTime dt_First = dt.AddDays(1 - (dt.Day));
            //获得某年某月的天数  
            int year = dt.Date.Year;
            int month = dt.Date.Month;
            int dayCount = DateTime.DaysInMonth(year, month);
            //本月最后一天时间  
            DateTime dt_Last = dt_First.AddDays(dayCount - 1);

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值