日历数据集合

    技术2022-07-06  217

    制作日历, 生成日历数据集合。

    有了数据可以随便加工。

     

    如:按照周6,7设置工作日与非工作日,颜色显示。

     

    public class MakeCalender { string spaceStr = ""; public List<WeekDay> GetSet(int year, int month) { int sumDays = GetDaysOfMonth(year, month); int spaceNum = GetWeek(year, month, 1); //每月的1号是星期几 int sumLine = (sumDays + spaceNum) / 7; sumLine = ((sumDays + spaceNum) % 7 == 0) ? sumLine : (sumLine + 1); int temUum = 0;//for space fill(temUum<spaceNum) int nowDay = 1;// all the month Color colorWhite = Color.White; List<WeekDay> weekDayList = new List<WeekDay>(); for (int i = 0; i < sumLine; i++) { WeekDay weekDayEntity = new WeekDay(); //第一行 if (i == 0) { GetFirstWeekDay(spaceNum, ref temUum, ref nowDay, weekDayEntity); weekDayList.Add(weekDayEntity); continue; } for (int j = 1; j <= 7; j++) { GetNextWeekDay(sumDays, ref nowDay, weekDayEntity, j); } weekDayList.Add(weekDayEntity); } return weekDayList; } /// <summary> /// /// </summary> /// <param name="spaceNum"></param> /// <param name="temUum"></param> /// <param name="nowDay"></param> /// <param name="weekDayEntity"></param> private void GetFirstWeekDay(int spaceNum, ref int temUum, ref int nowDay, WeekDay weekDayEntity) { #region 日 if (temUum < spaceNum) { weekDayEntity.Sunday = spaceStr; weekDayEntity.IsWorkDay = true; temUum++; } else { weekDayEntity.Sunday = nowDay.ToString(); nowDay++; temUum++; } #endregion #region 一 if (temUum < spaceNum) { weekDayEntity.Monday = spaceStr; weekDayEntity.IsWorkDay = true; temUum++; } else { weekDayEntity.Monday = nowDay.ToString(); nowDay++; temUum++; } #endregion #region 二 if (temUum < spaceNum) { weekDayEntity.Tuesday = spaceStr; weekDayEntity.IsWorkDay = true; temUum++; } else { weekDayEntity.Tuesday = nowDay.ToString(); nowDay++; temUum++; } #endregion #region 三 if (temUum < spaceNum) { weekDayEntity.Wednesday = spaceStr; weekDayEntity.IsWorkDay = true; temUum++; } else { weekDayEntity.Wednesday = nowDay.ToString(); nowDay++; temUum++; } #endregion #region 四 if (temUum < spaceNum) { weekDayEntity.Thursday = spaceStr; weekDayEntity.IsWorkDay = true; temUum++; } else { weekDayEntity.Thursday = nowDay.ToString(); nowDay++; temUum++; } #endregion #region 五 if (temUum < spaceNum) { weekDayEntity.Friday = spaceStr; weekDayEntity.IsWorkDay = true; temUum++; } else { weekDayEntity.Friday = nowDay.ToString(); nowDay++; temUum++; } #endregion #region 六 if (temUum < spaceNum) { weekDayEntity.Saturday = spaceStr; weekDayEntity.IsWorkDay = true; temUum++; } else { weekDayEntity.Saturday = nowDay.ToString(); nowDay++; temUum++; } #endregion } /// <summary> /// /// </summary> /// <param name="sumDays"></param> /// <param name="nowDay"></param> /// <param name="weekDayEntity"></param> /// <param name="j"></param> private void GetNextWeekDay(int sumDays, ref int nowDay, WeekDay weekDayEntity, int j) { switch (j) { #region 日 case 1://日 { if (nowDay <= sumDays) { weekDayEntity.Sunday = nowDay.ToString(); nowDay++; } else { weekDayEntity.IsWorkDay = true; weekDayEntity.Sunday = spaceStr; } break; } #endregion #region 一 case 2: { if (nowDay <= sumDays) { weekDayEntity.Monday = nowDay.ToString(); nowDay++; } else { weekDayEntity.IsWorkDay = true; weekDayEntity.Monday = spaceStr; } break; } #endregion #region 二 case 3: { if (nowDay <= sumDays) { weekDayEntity.Tuesday = nowDay.ToString(); nowDay++; } else { weekDayEntity.IsWorkDay = true; weekDayEntity.Tuesday = spaceStr; } break; } #endregion #region 三 case 4: { if (nowDay <= sumDays) { weekDayEntity.Wednesday = nowDay.ToString(); nowDay++; } else { weekDayEntity.IsWorkDay = true; weekDayEntity.Wednesday = spaceStr; } break; } #endregion #region 四 case 5: { if (nowDay <= sumDays) { weekDayEntity.Thursday = nowDay.ToString(); nowDay++; } else { weekDayEntity.IsWorkDay = true; weekDayEntity.Thursday = spaceStr; } break; } #endregion #region 五 case 6: { if (nowDay <= sumDays) { weekDayEntity.Friday = nowDay.ToString(); nowDay++; } else { weekDayEntity.IsWorkDay = true; weekDayEntity.Friday = spaceStr; } break; } #endregion #region 六 case 7: { if (nowDay <= sumDays) { weekDayEntity.Saturday = nowDay.ToString(); nowDay++; } else { weekDayEntity.IsWorkDay = true; weekDayEntity.Saturday = spaceStr; } break; } #endregion } } /// <summary> /// 计算输入日期的星期 /// </summary> /// <param name="year">年份,yyyy格式 如:2009</param> /// <param name="month">月份:1到12之间的整数</param> /// <param name="day">日期:1到31之间的整数</param> /// <returns>星期数,1至7之间 1代表星期一,7代表星期天</returns> private int GetWeek(int year, int month, int day) { if (month == 1 || month == 2) { month += 12; year -= 1; } int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7; return (week + 1); } /// <summary> /// 取得月份的天数 /// </summary> /// <param name="theYear"></param> /// <param name="theMonth"></param> /// <returns></returns> private int GetDaysOfMonth(int theYear, int theMonth) { int days = 0; switch (theMonth) { case 1: days = 31; break; case 2: if (DateTime.IsLeapYear(theYear)) { //闰年二月为29天 days = 29; } else { //不是闰年,二月为28天 days = 28; } break; case 3: days = 31; break; case 4: days = 30; break; case 5: days = 31; break; case 6: days = 30; break; case 7: days = 31; break; case 8: days = 31; break; case 9: days = 30; break; case 10: days = 31; break; case 11: days = 30; break; case 12: days = 31; break; } return days; } public class WeekDay { public string Monday { get; set; } public string Tuesday { get; set; } public string Wednesday { get; set; } public string Thursday { get; set; } public string Friday { get; set; } public string Sunday { get; set; } public string Saturday { get; set; } //public Color MondayColor { get; set; } //public Color TuesdayColor { get; set; } //public Color WednesdayColor { get; set; } //public Color ThursdayColor { get; set; } //public Color FridayColor { get; set; } //public Color SundayColor { get; set; } //public Color SaturdayColor { get; set; } public bool IsWorkDay { get; set; } } }

     

     


    最新回复(0)