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

后端 未结 4 527
猫巷女王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: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

    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
    return time<=end
    

提交回复
热议问题