C# 根据日期计算日期周数

  当我们知道一个日期后,有的时候想知道当前日期是当年的第几周(如我们做周报计划什么的就可能遇到),一周的第一天是周几有两种说法,有的是以周一围一周的第一天,有的是以周日为一周的第一天,下面根据两种情况分别来计算

 

一、以周日为一周的第一天

        ///


        /// 根据日期计算日期周数(以周日为一周的第一天)
        ///

        /// 日期
        /// 日期周数
        private int WeekOfYear(DateTime date)
        {
           
            DayOfWeek dw = (Convert.ToDateTime(string.Format("{0}-1-1 0:0:0", date.Year.ToString()))).DayOfWeek;
            int day = 0;
            switch (dw)
            {
                case DayOfWeek.Sunday:
                    {
                        day = -1;
                        break;
                    }
                case DayOfWeek.Monday:
                    {
                        day = 0;
                        break;
                    }
                case DayOfWeek.Tuesday:
                    {
                        day = 1;
                        break;
                    }
                case DayOfWeek.Wednesday:
                    {
                        day = 2;
                        break;
                    }
                case DayOfWeek.Thursday:
                    {
                        day = 3;
                        break;
                    }
                case DayOfWeek.Friday:
                    {
                        day = 4;
                        break;
                    }
                case DayOfWeek.Saturday:
                    {
                        day = 5;
                        break;
                    }
            }
            int week = (date.DayOfYear + day) / 7 + 1;

            return week;
        }

 

则调用方法WeekOfYear就可以得到以周日为一周第一天的日期周数了

 

 

二、以周一为一周的第一天

 

///


        /// 根据日期计算日期周数(以周一为一周的第一天)
        ///

        /// 日期
        /// 日期周数
        private int WeekOfYear2(DateTime date)
        {

            DayOfWeek dw = (Convert.ToDateTime(string.Format("{0}-1-1 0:0:0", date.Year.ToString()))).DayOfWeek;
            int day = 0;
            switch (dw)
            {
               
                case DayOfWeek.Monday:
                    {
                        day = -1;
                        break;
                    }
                case DayOfWeek.Tuesday:
                    {
                        day = 0;
                        break;
                    }
                case DayOfWeek.Wednesday:
                    {
                        day = 1;
                        break;
                    }
                case DayOfWeek.Thursday:
                    {
                        day = 2;
                        break;
                    }
                case DayOfWeek.Friday:
                    {
                        day = 3;
                        break;
                    }
                case DayOfWeek.Saturday:
                    {
                        day = 4;
                        break;
                    }
                case DayOfWeek.Sunday:
                    {
                        day = 5;
                        break;
                    }
            }
            int week = (date.DayOfYear + day) / 7 + 1;

            return week;
        }

则调用方法WeekOfYear2就可以计算以周一为一周第一天的日期周数了。

你可能感兴趣的:(C#)