时间 日历 判断闰年或者平年

    /// <summary>
    /// 根据所选时间(年和月)得到当月日历表
    /// </summary>
    public class CalenderDT
    {
        public DataTable Convert(int year, int month)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("星期日", typeof(String));
            dt.Columns.Add("星期一", typeof(String));
            dt.Columns.Add("星期二", typeof(String));
            dt.Columns.Add("星期三", typeof(String));
            dt.Columns.Add("星期四", typeof(String));
            dt.Columns.Add("星期五", typeof(String));
            dt.Columns.Add("星期六", typeof(String));
            DataRow dr;
            int days = GetDaysOfMonth(year, month);
            int firstDayOfMonth = GetWeekOfDate(year, month, 1);
            dr = dt.NewRow();
            for (int i = 1; i <= days; i++)
            {
                dr[(firstDayOfMonth+i-1) % 7] = i;
                if (GetWeekOfDate(year, month, i) == 6)
                {
                    dt.Rows.Add(dr);
                    dr = dt.NewRow();
                }
            }
            dt.Rows.Add(dr);
            return dt;
        }

        #region 判断是否为闰年
        public static bool IsleapYear(int year)
        {
            if ((year % 4 == 0 && year % 100 != 100) || year % 400 == 0)
            {
                return true;
            }
            else
                return false;
        }
        #endregion

        #region 判断一个月有多少天
        public static int GetDaysOfMonth(int year, int month)
        {
            int days = 0;

            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;
                    break;
                case 2:
                    days = IsleapYear(year) ? 29 :28;
                    break;
                default:
                    days = 30;
                    break;
            }
            return days;
        }
        #endregion

        #region 判断一年有多少天
        public static int GetDaysOfYear(int year)
        {
            return IsleapYear(year) ? 366 : 365;
        }
        #endregion

        #region 时间的星期
        public static int GetWeekOfDate(int year, int month, int day)
        {
            int days = GetDaysOfDate(year, month, day);
            return (days + 2) % 7;
        }
        #endregion

        #region 判断该日是星期几
        public static string GetWeek(string str)
        {
            string[] strs = str.Split('-');
            int week=GetWeekOfDate(int.Parse(strs[0]), int.Parse(strs[1]), int.Parse(strs[2]));
            switch (week)
            {
                case 1:
                    return "星期一";
                case 2:
                    return "星期二";
                case 3:
                    return "星期三";
                case 4:
                    return "星期四";
                case 5:
                    return "星期五";
                case 6:
                    return "星期六";
                case 7:
                    return "星期日";
                default:
                    return "星期日";
            }
        }
        #endregion

        #region 距离1980年有多少天
        public static int GetDaysOfDate(int year, int month, int day)
        {
            int totalDays = 0;
            for (int i = 1980; i < year; i++)
            {
                totalDays = totalDays + GetDaysOfYear(i);
            }
            for (int i = 1; i < month; i++)
            {
                totalDays += GetDaysOfMonth(year, i);
            }
            totalDays = totalDays + day - 1;
            return totalDays ;
        }
        #endregion
    }

  

posted @ 2013-08-30 16:09  三叶草╮  阅读(397)  评论(0编辑  收藏  举报