由于工作需求写的一个程序,判断某一年所有的放假日期,根据国家的法定假日和补休日期进行的判断。
protected void Button1_Click(object sender, EventArgs e) { string sql = ""; //2015年1月1日的星期数 int year = 2015; int week = 4; for (int month = 1; month <= 12; month++) { int day = 31; switch (month) { case 2: if (IsLeapYear(year)) { day = 29; } else { day = 28; } break; case 4: day = 30; break; case 6: day = 30; break; case 9: day = 30; break; case 11: day = 30; break; } for (int a = 1; a <= day; a++) { string time = string.Format("{0}-{1}-{2}", year, month, a); if (week == 7 || checkfangjia(time)) { if (!bufangjia(time)) { string m = string.Format("INSERT INTO [dbo].[tm_holidays] ([id], [year], [date]) VALUES (N'" + year + "', N'{0} 00:00:00.000');</br>", time); sql += m; Response.Write(m); //Response.Write("2015年" + month + "月" + a + "日" + "星期" + week + "</br>"); } } if (week == 7) { week = 1; } else { week++; } } } } //判断某一天是否放假 public bool checkfangjia(string day) { //所有放假的日期 string[] fangjia = { "2015-1-1", "2015-1-2", "2015-1-3", "2015-2-18", "2015-2-19", "2015-2-20", "2015-2-21", "2015-2-22", "2015-2-23", "2015-2-24", "2015-4-4", "2015-4-5", "2015-4-6", "2015-5-1", "2015-5-2", "2015-5-3", "2015-6-20", "2015-6-21", "2015-6-22", "2015-9-26", "2015-9-27", "2015-10-1", "2015-10-2", "2015-10-3", "2015-10-4", "2015-10-5", "2015-10-6", "2015-10-7" }; foreach (string s in fangjia) { if (s.Equals(day)) { return true; } } return false; } //判断某一天是否调休 public bool bufangjia(string day) { //与周六 周日调休的日期 string[] bufangjia = { "2015-1-4", "2015-2-15", "2015-2-28", "2015-10-10" }; foreach (string s in bufangjia) { if (s.Equals(day)) { return true; } } return false; } //判断平年还是闰年 private bool IsLeapYear(int year) { if (year % 100 == 0) { if (year % 400 == 0) { return true; } } else { if (year % 4 == 0) { return true; } } return false; }