用c#写万年历(简单版)

using System;
using System.Collections.Generic;
using System.Text;

namespace CalendarProjects
{
    class Calendar
    {
        private string[] WeekDay = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        private int[] pingNian = new int[] { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        private int[] leapYear = new int[] { 0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        ///


        ///计算两个日期相差的天数
        ///

        /// 一个比较小的日期
        /// 一个比较大的日期
        /// 相差的天数
        public int DiffDays(DateTime pre, DateTime next)
        {
            int days = 0;
            days = (next.Year - pre.Year) * 365 + GetDays(next) - GetDays(pre);
            for (int i = pre.Year + 1; i < next.Year; i++)
                if (IsLeapYear(i)) days++;
            if (IsLeapYear(next.Year) && next.Month >= 2 && next.Day >= 29) days--;
            return days+1;
        }
        ///
        /// 计算日期在本年的天数
        ///

        /// 一个日期型数据
        /// 天数
        public int GetDays(DateTime dateTime)
        {
            int days = 0;
            int year = dateTime.Year;
            int month = dateTime.Month;
            int day = dateTime.Day;
            if (IsLeapYear(year))
            {
                for (int i = 1; i < month; i++)
                    days += leapYear[i];
            }
            else
            {
                for (int i = 1; i < month; i++)
                    days += pingNian[i];
            }
            days += day;
            return days;
        }
        ///
        /// 计算这个日期是星期几
        ///

        /// 目标日期
        /// 星期几
        public string GetWeekDays(DateTime time)
        {
            DateTime Temp = new DateTime(1900, 1, 1);
            int days = DiffDays(Temp, time);
            return WeekDay[days % 7];
        }
        ///
        /// 计算距基准日期相隔多少天后的日期是多少
        ///

        /// 基准日期
        /// 相距天数
        /// 日期
        public DateTime GetTime(DateTime time, int diffDays)
        {
            int month = 0;
            int year = diffDays / 365;
            int days = diffDays % 365;
            for (int i = 1; i < time.Month; i++)
            {
                days += pingNian[i];
            }
            days += time.Day;
            for (int i = time.Year; i < time.Year + year; i++)
            {
                if (IsLeapYear(i)) days--;
            }
            if (IsLeapYear(time.Year + year) && days > 366)
            {
                days -= 366;
                year++;
            }
            if (!IsLeapYear(time.Year + year) && days > 365)
            {
                year++;
                days -= 365;
            }
            if (IsLeapYear(time.Year + year))
            {
                for (int i = 1; i < 12; i++)
                {
                    if (days > leapYear[i]) days -= leapYear[i];
                    else
                    {
                        month = i;
                        break;
                    }
                }
            }
            else
            {
                for (int i = 1; i < 12; i++)
                {
                    if (days > pingNian[i]) days -= pingNian[i];
                    else
                    {
                        month = i;
                        break;
                    }
                }
            }
            return new DateTime(time.Year + year, month, days);
        }
        ///
        /// 判断是否是闰年
        ///

        /// 测试年份
        /// True;false
        public static bool IsLeapYear(int thisYear)
        {
            if (thisYear % 4 == 0 && thisYear % 100 != 0 || thisYear % 400 == 0) return true;
            return false;
        }

    }
}

你可能感兴趣的:(用c#写万年历(简单版))