iOS 比较两个时间大小

- (int)compareOneDay:(NSDate*)oneDay withAnotherDay:(NSDate*)anotherDay

{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yy年MM月dd日 HH:mm"];

    NSString*oneDayStr = [dateFormatter stringFromDate:oneDay];

    NSString*anotherDayStr = [dateFormatter stringFromDate:anotherDay];

    NSDate*dateA = [dateFormatter dateFromString:oneDayStr];

    NSDate*dateB = [dateFormatter dateFromString:anotherDayStr];

    NSComparisonResult result = [dateA compare:dateB];

    if (result == NSOrderedDescending) {

        //在指定时间前面 过了指定时间 过期

        return 1;

    }

    elseif(result ==NSOrderedAscending){

        //没过指定时间 没过期

        //NSLog(@"Date1 is in the past");

        return-1;

    }

    //刚好时间一样.

    //NSLog(@"Both dates are the same");

    return 0;


}

你可能感兴趣的:(iOS 比较两个时间大小)