简体   繁体   English

当前时间是否在范围内

[英]Is current time within range

I know this question has been asked manytimes but mine has a small twist. 我知道这个问题已经被问过很多次了,但是我的想法有点曲折。 There are many different shifts at work and I have two string shiftStart and shiftEnd . 工作中有许多不同的班次,我有两个字符串shiftStartshiftEnd (example "6:00:00 PM" & "03:00:00 AM" respectively). (例如分别为“ 6:00:00 PM”和“ 03:00:00 AM”)。 This represents noon to morning. 这代表从中午到早晨。 I am trying to make a function which tells me if DateTime.Now is within this timeperiod. 我正在尝试创建一个告诉我DateTime.Now是否在此时间段内的函数。

Note: the shift can be anything ... morning to noon, noon to morning, no fixed duration (like 8 hours). 注意: 班次可以是任何东西 ...从早上到中午,从中午到早上, 没有固定的持续时间(例如8个小时)。

static bool NowWithinShiftTime("6:00:00 PM", "03:00:00 AM")

Following is what I have tried but no matter what I do I cannot seem to find the logic... Please help me out... 以下是我尝试过的内容,但是无论我做什么,我似乎都找不到逻辑...请帮助我...

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

You could stick with TimeSpan : 您可以坚持使用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;
}

Firstly, make all the business logic deal just with time-related types. 首先,使所有业务逻辑只处理与时间相关的类型。 (Personally I'd use my Noda Time library with its LocalTime type for this work, but...) The parsing can come in the calling code. (我个人将Noda Time库及其LocalTime类型用于此工作,但是...)解析可以在调用代码中进行。

The logic is reasonably simple: if the "end time" is actually before the "start time" then just switch the two round and invert the result. 逻辑很简单:如果“结束时间”实际上早于“开始时间”,则只需将两者进行切换并将结果取反即可。 After all, you're just dividing the day into "shift time" and "not shift time"... and a shift of (say) 8pm to 3am is "on" whenever a shift of 3am to 8pm is "off". 毕竟,您只是将日期分为“轮班时间”和“非轮班时间” ...,只要“凌晨3点”到“ 8pm”是“关闭”,则将(例如)从8pm转变为“ on”。

For example: 例如:

public bool NowWithinShiftTime(TimeSpan startTimeOfDay, TimeSpan endTimeOfDay)
{
    if (startTimeOfDay > endTimeOfDay)
    {
        return !NowWithinShiftTime(endTimeOfDay, startTimeOfDay);
    }
    TimeSpan now = DateTime.Now.TimeOfDay;
    return startTimeOfDay <= now && now < endTimeOfDay;
}

Notes: 笔记:

  • I've made this deliberately half-closed, as that tends to be appropriate for intervals so that if you have abutting intervals, any given value falls into exactly one. 我故意将其设置为半封闭式,因为它通常适合于间隔,因此,如果您有相邻的间隔,则任何给定的值都将恰好等于1。 Check exactly what you want, although it'll only be relevant for those two exact points in time per day. 精确检查您想要什么,尽管它仅与每天的这两个确切时间点相关。
  • The code is currently hard to test: I'd encourage you to abstract out the idea of "a clock" into an interface, so you can use a fake clock for testing. 该代码目前很难测试:我鼓励您将“时钟”的概念抽象到接口中,以便可以使用伪造的时钟进行测试。 (Noda Time does this for you, and provides an appropriate FakeClock .) (Noda Time为您执行此操作,并提供适当的FakeClock 。)

I believe you're 90% there: 我相信您在那里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;
    }
}

Here's an ideone to show it works: http://ideone.com/rdb867 这是一个显示效果的ideone: http ://ideone.com/rdb867

This should work 这应该工作

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

This should work: 这应该工作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM