[英]Is current time within range
我知道这个问题已经被问过很多次了,但是我的想法有点曲折。 工作中有许多不同的班次,我有两个字符串shiftStart和shiftEnd 。 (例如分别为“ 6:00:00 PM”和“ 03:00:00 AM”)。 这代表从中午到早晨。 我正在尝试创建一个告诉我DateTime.Now是否在此时间段内的函数。
注意: 班次可以是任何东西 ...从早上到中午,从中午到早上, 没有固定的持续时间(例如8个小时)。
static bool NowWithinShiftTime("6:00:00 PM", "03:00:00 AM")
以下是我尝试过的内容,但是无论我做什么,我似乎都找不到逻辑...请帮助我...
static bool NowWithinShiftTime(string shiftStart, string shiftEnd)
{
DateTime startDate;
DateTime endDate;
DateTime now = DateTime.Now;
TimeSpan startTime = DateTime.Parse(shiftStart).TimeOfDay;
TimeSpan endTime = DateTime.Parse(shiftEnd).TimeOfDay;
if (startTime < endTime) // same day
{
startDate = new DateTime(now.Year, now.Month, now.Day) + startTime;
endDate = new DateTime(now.Year, now.Month, now.Day) + endTime;
}
else // next day
{
startDate = new DateTime(now.Year, now.Month, now.AddDays(-1).Hour) + startTime;
endDate = DateTime.Today.AddDays(1) + endTime;
}
if (now >= startDate && now <= endDate)
{
return true;
}
else
{
return false;
}
}
您可以坚持使用TimeSpan
:
static bool NowWithinShiftTime(string shiftStart, string shiftEnd)
{
TimeSpan startTime = DateTime.Parse(shiftStart).TimeOfDay;
TimeSpan endTime = DateTime.Parse(shiftEnd).TimeOfDay;
TimeSpan now = DateTime.Now.TimeOfDay;
if (startTime < endTime)
return now >= startTime && now <= endTime;
else
return now >= startTime || now <= endTime;
}
首先,使所有业务逻辑只处理与时间相关的类型。 (我个人将Noda Time库及其LocalTime
类型用于此工作,但是...)解析可以在调用代码中进行。
逻辑很简单:如果“结束时间”实际上早于“开始时间”,则只需将两者进行切换并将结果取反即可。 毕竟,您只是将日期分为“轮班时间”和“非轮班时间” ...,只要“凌晨3点”到“ 8pm”是“关闭”,则将(例如)从8pm转变为“ on”。
例如:
public bool NowWithinShiftTime(TimeSpan startTimeOfDay, TimeSpan endTimeOfDay)
{
if (startTimeOfDay > endTimeOfDay)
{
return !NowWithinShiftTime(endTimeOfDay, startTimeOfDay);
}
TimeSpan now = DateTime.Now.TimeOfDay;
return startTimeOfDay <= now && now < endTimeOfDay;
}
笔记:
FakeClock
。) 我相信您在那里90%:
static bool NowWithinShiftTime(string shiftStart, string shiftEnd)
{
DateTime startDate;
DateTime endDate;
DateTime now = DateTime.Now;
TimeSpan startTime = DateTime.Parse(shiftStart).TimeOfDay;
TimeSpan endTime = DateTime.Parse(shiftEnd).TimeOfDay;
if (startTime < endTime) // same day
{
startDate = DateTime.Today + startTime;
endDate = DateTime.Today + endTime;
}
else // next day
{
startDate = DateTime.Today + startTime;
endDate = DateTime.Today.AddDays(1) + endTime;
}
if (now >= startDate && now <= endDate)
{
return true;
}
else
{
return false;
}
}
这是一个显示效果的ideone: http ://ideone.com/rdb867
这应该工作
static bool NowWithinShiftTime(string shiftStart, string shiftEnd)
{
DateTime now = DateTime.Now;
DateTime startDate = DateTime.Parse(shiftStart);
DateTime endDate = DateTime.Parse(shiftEnd);
if (endDate < startDate)
{
endDate = endDate.AddDays(1);
}
return now >= startDate && now <= endDate;
}
这应该工作:
bool IsWithinTimeRange(DateTime dateToCompare, DateTime startDate, DateTime endDate)
{
TimeSpan timeToCompare = dateToCompare.TimeOfDay;
TimeSpan start = startDate.TimeOfDay;
TimeSpan end = endDate.TimeOfDay;
if (start < end)
return timeToCompare >= start && timeToCompare <= end;
else
return !(end < timeToCompare && timeToCompare < start);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.