根据节假日表分析当前日期是否属于节假日配置表的时间以及判定当前日期是否属于双休日
首先建一张节假日表TRA_PricingHoliday ID 节假日时间 HDate 是否工作日 HType 2表示不上班 非工作日,1表示正常上班工作日
判定代码:
private string _strWorkingDayAM = "00:00";//工作时间 private string _strWorkingDayPM = "23:59"; private string _strRestDay = "6,7";//周几休息日 周六周日为 6,7 private TimeSpan dspWorkingDayAM;//工作时间 private TimeSpan dspWorkingDayPM;//下午工作时间 clsArrayList myArrayList; /// <summary> /// 是否周休日 /// </summary> /// <returns></returns> private bool m_IsWorkingDay() { string strWeekNow = this.m_GetWeekNow();//当前周几 //判断是否休息日 for (int i = 0; i < myArrayList.p_Count; i++) { if (strWeekNow == myArrayList.m_GetItem(i)) { return false; } } //判断当前时间是否在工作时间段内 TimeSpan dspNow = DateTime.Now.TimeOfDay; //string date = DateTime.Now.ToShortDateString(); if (dspNow > dspWorkingDayAM && dspNow < dspWorkingDayPM) { return true;//是在工作时间内 } return false;//不是在工作时间内 } /// <summary> /// 初始化默认值 /// </summary> private void m_InitWorkingDay() { dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay; dspWorkingDayPM = DateTime.Parse(_strWorkingDayPM).TimeOfDay; myArrayList = new clsArrayList(_strRestDay, ','); } /// <summary> /// 获取当前周几 /// </summary> /// <returns></returns> private string m_GetWeekNow() { string strWeek = DateTime.Now.DayOfWeek.ToString(); switch (strWeek) { case "Monday": return "1"; case "Tuesday": return "2"; case "Wednesday": return "3"; case "Thursday": return "4"; case "Friday": return "5"; case "Saturday": return "6"; case "Sunday": return "7"; } return "0"; } /// <summary> /// 初始化1是工作日 2 是非工作日 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmManagement_Load(object sender, EventArgs e) { this.m_InitWorkingDay(); bool bol = this.m_IsWorkingDay(); int resultYesOrNo = GetIsWorkDate(); //双休日和工作日判断 if ((bol.ToString().Equals(true)) && resultYesOrNo == 1) { this.labelX1.Text = "今天是工作日非节假日"; } else if (resultYesOrNo == 1) { this.labelX1.Text = "今天是工作日非节假日"; } else if ((bol.Equals(true))) { this.labelX1.Text = "今天是工作日"; } else { this.labelX1.Text = "今天是节假日非工作日"; } } /// <summary> /// 获取是否工作日的类型 1工作日 2 非工作日 /// </summary> /// <returns></returns> public int GetIsWorkDate() { string date = DateTime.Now.ToString("yyyy-MM-dd"); string sqlWork = "SELECT HType FROM TRA_PricingHoliday WHERE HDate='" + date + "'"; int iresult = 0; string htype = null; try { DataTable dt = helper.Selectinfo(sqlWork); for (int i = 0; i < dt.Rows.Count; i++) { //从数据库得到字段的值 htype = dt.Rows[0]["HType"].ToString(); } if (htype=="1") { return iresult=1; } if (htype == "2") { return iresult =2; } } catch { } return iresult; }
使用类:
public class clsArrayList { public int p_Count = -1; //数据组数 private string[] myArray1; //一维矩阵 private string[,] myArray2; //二维矩阵 /// <summary> /// 一维字符串分隔,初始化.例: clsArrayList myArray1List=new clsArrayList("1;2;3;4;5",';') /// </summary> /// <param name="strStringSource"></param> /// <remarks></remarks> public clsArrayList(string strStringSource, char charSepartor) { try { myArray1 = strStringSource.Split(charSepartor);//截取 p_Count = myArray1.Length; } catch (Exception ex) { clsLogHelper.m_CreateErrorLogTxt("clsArrayList(" + strStringSource + " , " + charSepartor + ")", "", ex.Message.ToString()); } } /// <summary> /// 二维字符串分隔,初始化. 例: clsArrayList myArray1List=new clsArrayList("1,A;2,B;3,C;4,D;5,E" , ';' , ',') /// </summary> /// <param name="strStringSource"></param> /// <param name="strColumnSepartor"></param> /// <param name="strRowSepartor"></param> /// <remarks></remarks> public clsArrayList(string strStringSource, char charColumnSepartor, char charRowSepartor) { try { string[] strColumn; //第一次截取 ";" string[] strRow; //第二次截取 "," strColumn = strStringSource.Split(charColumnSepartor);//第一次截取 p_Count = strColumn.Length; myArray2 = new string[p_Count, 2]; for (int i = 0; i <= p_Count - 1; i++) //将数据保存 数组 myArray1 中 { strRow = strColumn[i].Split(charRowSepartor);//第二次截取 myArray2[i, 0] = strRow[0]; myArray2[i, 1] = strRow[1]; } } catch (Exception ex) { clsLogHelper.m_CreateErrorLogTxt("clsArrayList(" + strStringSource + " , " + charColumnSepartor + " , " + charRowSepartor + ")", "", ex.Message.ToString()); } } /// <summary> /// 一维字符串,取值 /// </summary> /// <param name="intIndex"></param> /// <returns></returns> public string m_GetItem(int intIndex) { if (myArray1 == null) { return ""; } if (p_Count == -1) { return ""; } if (intIndex > p_Count - 1) { return ""; } else { return myArray1[intIndex]; } } /// <summary> /// 二维字符串,取值 /// </summary> /// <param name="intKeyIndex"></param> /// <param name="intValueIndex"></param> /// <returns></returns> /// <remarks></remarks> public string m_GetItem(int intKeyIndex, int intValueIndex) { if (myArray2 == null) { return ""; } if (p_Count == -1) { return ""; } if (intKeyIndex > p_Count - 1 || intValueIndex > 1)//Value为列,二维 { return ""; } else { return myArray2[intKeyIndex, intValueIndex]; } } /// <summary> /// 二维字符串,通过Value 获取Key ,例: m_GetKey("1") /// </summary> /// <param name="strValue"></param> /// <returns></returns> /// <remarks></remarks> public string m_GetKey(string strValue) { try { if (myArray2 == null) { return ""; } if (p_Count == -1) { return ""; } for (int i = 0; i <= p_Count - 1; i++) //通过value查找key { if (myArray2[i, 1] == strValue) { return myArray2[i, 0]; //返回key的值 } } return ""; } catch (Exception ex) { clsLogHelper.m_CreateErrorLogTxt("clsArrayList(" + strValue + ")", "", ex.Message.ToString()); return ""; } } /// <summary> /// 二维字符串,通过Key 获取Value /// </summary> /// <param name="strKey"></param> /// <returns></returns> /// <remarks></remarks> public string m_GetValue(string strKey) { try { if (myArray2 == null) { return ""; } if (p_Count == -1) { return ""; } for (int i = 0; i <= p_Count - 1; i++) //通过key查找value { if (myArray2[i, 0] == strKey) { return myArray2[i, 1]; //返回value的值 } } return ""; } catch (Exception ex) { clsLogHelper.m_CreateErrorLogTxt("m_GetValue(" + strKey + ")", "", ex.Message.ToString()); return ""; } }