星期几以及周,月份天数的计算(共用方法)

        #region 计算星期几
        /// <summary>
         ///  //: 星期日至星期六的值为0-6 
         /// </summary>
         /// <param name="year"></param>
         /// <param name="month"></param>
         /// <param name="day"></param>
         /// <returns></returns>   
        public static int GetWeeksOfDate(int year, int month, int day)  
        {
            return GetWeeksOfDate(new DateTime(year, month, day));
        }

        public static int GetWeeksOfDate(DateTime time)
        {
            DayOfWeek d = time.DayOfWeek;
            return Convert.ToInt32(d);  
        }

        public static string GetWeeksOfDateForChinese(DateTime time)
        {
            int week = GetWeeksOfDate(time);
            string strWeek ="";
            if (week == 0)
            {
                strWeek = "";
            }
            else {
                strWeek =Tools.Number2Chinese(week);
            }
            return "" + strWeek;
        }
        /// <summary>
        /// 星期一至星期日的值为1-7 
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int GetNumOfWeeks(DateTime time)
        {
            int t=GetWeeksOfDate(time);
            if(t==0)
            {
                t = 7;
            }
            return t;
        }
        #endregion 
 #region 获取指定年月的天数
        /// <summary>
        /// 获取指定年月的天数 
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns> 
        public static int GetDaysOfMonth(int year, int month)  
        {  
            DateTime dtCur = new DateTime(year, month, 1);  
            int days = dtCur.AddMonths(1).AddDays(-1).Day;  
            return days;
        }
        #endregion 

 

posted @ 2019-09-23 21:05  醉酒三分醒  阅读(720)  评论(0编辑  收藏  举报