How can I check if the current time is between in a time frame?

后端 未结 4 518
猫巷女王i
猫巷女王i 2020-12-02 15:50

I have a service that user can configure to run during \"off-peak\" hours. They have the ability to set the time frame that the service can run.

For Example:

相关标签:
4条回答
  • 2020-12-02 16:30

    If startTime and endTime represent a single time interval (it will only happen once, and startTime and endTime represent the date and the time to start/stop), then it's as easy as saying

    bool isTimeBetween = someTime >= startTime && someTime <= endTime;
    

    If it's a recurring event (happens every day, during some interval), you can do comparisons using the TimeOfDay property. (The recurring case is the one where you have to consider a start/stop that crosses midnight)

    static public bool IsTimeOfDayBetween(DateTime time, 
                                          TimeSpan startTime, TimeSpan endTime)
    {
        if (endTime == startTime)
        {
            return true;   
        }
        else if (endTime < startTime)
        {
            return time.TimeOfDay <= endTime ||
                time.TimeOfDay >= startTime;
        }
        else
        {
            return time.TimeOfDay >= startTime &&
                time.TimeOfDay <= endTime;
        }
    
    }
    

    (Note: This code assumes that if start == end, then it covers all times. You made a comment to this effect on another post)

    For example, to check if it's between 5 AM and 9:30 PM

    IsTimeOfDayBetween(someTime, new TimeSpan(5, 0, 0), new TimeSpan(21, 30, 0))
    

    If startTime and endTime are DateTimes, you could say

    IsTimeOfDayBetween(someTime, startTime.TimeOfDay, endTime.TimeOfDay)
    
    0 讨论(0)
  • 2020-12-02 16:30
    DateTime t1;
    t1 = DateTime.Now;
    
    // loop inbetween start and end time      
    
    if (t1>=start_time &&t1<=end_time) 
    
    {
       //your code / action
    }
    
    //if you are using sql to get values 
    
    start_time = Convert.ToDateTime(row.Cells[10].Text);
    end_time = Convert.ToDateTime(row.Cells[11].Text);
    //convert them to string or you will get some error!!!
    
    0 讨论(0)
  • 2020-12-02 16:34

    So I assume from the question that you want to know if given a start time and end time for a day (not including the actual date, i.e., 1/1/1900 or something like that) to see if another time is with the time specified by start and end. E.g., if start is 9pm and end is 9am, accept 10pm but reject 10am.

    You can do this either per time range types (times are equal, end is after start, end is before start) which is simple:

    if (end==start) return true
    else if (end>start) return start<=time && time<=end
    else return !(time>end && time<start)
    

    Or you can extend the range of start and end such that end is always after start as such:

    if (end<=start) end += <24 hours>
    if (time<start) time+= <24 hours>
    return time<=end
    
    0 讨论(0)
  • 2020-12-02 16:42

    I'm assuming you are saving the start and end times in the applications configuration file, so all you basically have to do is have your application set a flag to "on" when the "start time" occurs and set it to "off" when the stop time occurs.

    That way you don't have to be constantly checking if "now" is "between start and end" times.

    0 讨论(0)
提交回复
热议问题