c# 判断当前时间是否在工作日时间段内

时间:2021-04-16

  下面是c# 判断当前时间是否在工作日时间段内的代码:

//获取当前周几
private string _strWorkingDayAM = "08:30";//工作时间上午08:00
private string _strWorkingDayPM = "17:30";
private string _strRestDay = "6,7";//周几休息日 周六周日为 6,7

private TimeSpan dspWorkingDayAM;//工作时间上午08:00
private TimeSpan dspWorkingDayPM;

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";
}

///
/// 判断是否在工作日内
///
///
private bool m_IsWorkingDay()
{
string strWeekNow = this.m_GetWeekNow();//当前周几
////判断是否有休息日
string[] RestDay = _strRestDay.Split(',');
if (RestDay.Contains(strWeekNow))
{
return false;
}

//判断当前时间是否在工作时间段内
dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay;
dspWorkingDayPM = DateTime.Parse(_strWorkingDayPM).TimeOfDay;

TimeSpan dspNow = DateTime.Now.TimeOfDay;
if (dspNow > dspWorkingDayAM && dspNow < dspWorkingDayPM)
{
return true;
}
return false;
}

//初始化默认值
private void m_InitWorkingDay()
{
dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay;
dspWorkingDayPM = DateTime.Parse(_strWorkingDayPM).TimeOfDay;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章