ios- Check whether current time is between two times or not

后端 未结 4 1660
后悔当初
后悔当初 2020-12-09 23:12

I want to check whether the current time is between two time or not.

For example, I want to check if the current time is between today morning 10AM to 12PM or Tomorr

4条回答
  •  心在旅途
    2020-12-09 23:54

    In Swift:

    let componets = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())
    let currentHour = componets.hour
    let currentMinute = componets.minute
    let currentSecond = componets.second
    
    if currentHour! < 7 || currentHour! > 21 {
        print ("do something")
    } else {
        print ("do nothing")
    }
    

提交回复
热议问题