检查当前时间是否在时间A和B之间

时间:2012-09-02 17:57:00

标签: iphone ios ipad time

无论如何,我可以检查当前时间是否介于两次之间?

当前时间=中午12:00 时间A:上午8:00
时间B:下午3:00

这将返回true,这不会:
当前时间:下午1:00
A = 1:00 AM
B =凌晨2:00

1 个答案:

答案 0 :(得分:3)

你可以按照以下方式做到这一点:

NSDate *timeA, *timeB; // Whatever and however you want to set these;
NSDate *now = [NSDate date];

If([now compare:timeA] == NSOrderedDescending && [now compare:timeB] == NSOrderedAscending)
    // then now is between timeA and Time B