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

后端 未结 4 1658
后悔当初
后悔当初 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:58

    Try this

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
    NSInteger currentHour = [components hour];
    NSInteger currentMinute = [components minute];
    NSInteger currentSecond = [components second];
    
    if (currentHour < 7 || (currentHour > 21 || currentHour == 21 && (currentMinute > 0 || currentSecond > 0))) {
        // Do Something
    }
    

    Replace 7, 21 with your time

    I get this from How to compare time

提交回复
热议问题