比较目标C中的时间和日期

kuc*_*ose 2 time xcode date objective-c

如何在目标C中进行比较,以查看某个时间和日期周期是否与已经在plist中的另一个重叠?

这最常用于预订/预订应用程序,以查看该特定时间段是否已被占用等.

Rav*_*shi 6

试试这个比较..

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss aa"];


NSDate *dateOne=[dateFormat dateFromString:@"01/09/2011 11:12:10 AM" ];
        NSDate *dateTwo = [NSDate date];

        switch ([dateOne compare:dateTwo]){
            case NSOrderedAscending:
                //NSLog(@"NSOrderedAscending");
                break;
            case NSOrderedSame:
                //NSLog(@"NSOrderedSame");
             {
break;
            case NSOrderedDescending:
                //NSLog(@"NSOrderedDescending");
                break;
        }

[dateFormat release];
Run Code Online (Sandbox Code Playgroud)