定义一个判断白班/夜班的方法
private bool IsNight(DateTime curDt, DateTime dtBeginTime, DateTime dtEndTime)
{
bool isNight = false;
if (dtBeginTime < dtEndTime && curDt >= dtBeginTime && curDt < dtEndTime)
{
//白班
isNight = false;
}
else if
{
//夜班
isNight = true;
}
return isNight;
}
然后,可以通过这种方式调用
var curDt = DateTime.Now;
DateTime dtBeginTime = DateTime.Parse(String.Format("{0} {1}", curDt.ToString_yyyyMMdd(),"08:30:00")); DateTime dtEndTime = DateTime.Parse(String.Format("{0} {1}", curDt.ToString_yyyyMMdd(),"20:30:00")); vmData.isNight = IsNight(curDt, dtBeginTime, dtEndTime);
直接获取当前时间,判断时间段就可以呀
return (DateTime.Now.TimeOfDay >= new TimeSpan(8, 30, 0) && DateTime.Now.TimeOfDay < new TimeSpan(20, 30, 0)) ? "白班" : "夜班";