how to check if time is within a specific range in swift

前端 未结 11 1247
一整个雨季
一整个雨季 2020-12-05 03:24

Hi I am trying to check if the current time is within a time range, say 8:00 - 16:30. My code below shows that I can obtain the current time as a string, but I am unsure how

11条回答
  •  情话喂你
    2020-12-05 03:51

    Here is some code I use in one of my current projects. Just set start time as 8:00, end time as 16:30, and timeStamp as current time.

    func isTimeStampCurrent(timeStamp:NSDate, startTime:NSDate, endTime:NSDate)->Bool{
        timeStamp.earlierDate(endTime) == timeStamp && timeStamp.laterDate(startTime) == timeStamp
    }
    

提交回复
热议问题