时间比较

2020-03-26  本文已影响0人  鄂北
+ (int)compareBeforeDay:(NSDate *)oneDay withLaterDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    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];
    NSLog(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //dateA>dateB
        return 1;
    }
    else if (result == NSOrderedAscending){
        //dateA<dateB
        return -1;
    }
    //dateA=dateB
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读