根据年月日 获取星期

/// <summary>根据日期,获得星期几</summary>
        /// <param name="y"></param>
        /// <param name="m"></param>
        /// <param name="d"></param>
        /// <returns>星期几,1代表星期一;7代表星期日</returns>
        private static int getWeekDay(int y, int m, int d)
        {
            if (m == 1) m = 13;
            if (m == 2) m = 14;
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
            return week;
        }

        /// <summary>
        /// 获取星期
        /// </summary>
        public static string GetWeek()
        {
            int m = System.DateTime.Today.Month;
            int y = System.DateTime.Today.Year;
            int d = System.DateTime.Today.Day;
            int weeks = getWeekDay(y, m, d);
            string Week = "";
            switch (weeks)
            {
                case 1:
                    Week = "周一";
                    break;
                case 2:
                    Week = "周二";
                    break;
                case 3:
                    Week = "周三";
                    break;
                case 4:
                    Week = "周四";
                    break;
                case 5:
                    Week = "周五";
                    break;
                case 6:
                    Week = "周六";
                    break;
                case 7:
                    Week = "周日";
                    break;
            }
            return Week;
        }

 

posted @ 2020-07-24 10:40  Sorry名米  阅读(122)  评论(0编辑  收藏  举报