美文网首页
时间比较

时间比较

作者: 鄂北 | 来源:发表于2020-03-26 17:33 被阅读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;
}

相关文章

网友评论

      本文标题:时间比较

      本文链接:https://www.haomeiwen.com/subject/mbncuhtx.html