zoukankan      html  css  js  c++  java
  • C#获取指定月指定周的日期范围 根据当前时间获取本月开始日期和结束日期

            /// <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;
            }


     

                //当前时间
                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);



     

  • 相关阅读:
    JSP_core
    J2EE_Servlet——helloworld
    JS_对象原型链
    JDBC_Hive & coding
    Python错误——TypeError: 'NoneType' object is not iterable
    Python错误——TypeError: is_leap_year() takes 0 positional arguments but 1 was given
    python错误:sh: cls: command not found
    Git——版本控制系统
    samba服务器搭建+权限设置
    windows10 samba 安全策略无法访问
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234398.html
Copyright © 2011-2022 走看看