博客> 日期比较
日期比较
2017-08-14 14:02 评论:1 阅读:210 vistatinchao

/比较两个日期大小 -(int)compareDate:(NSString)startDate withDate:(NSString)endDate{

int comparisonResult;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] init];
date1 = [formatter dateFromString:startDate];
date2 = [formatter dateFromString:endDate];
NSComparisonResult result = [date1 compare:date2];
NSLog(@"result==%ld",(long)result);
switch (result)
{
        //date02比date01大
    case NSOrderedAscending:
        comparisonResult = 1;
        break;
        //date02比date01小
    case NSOrderedDescending:
        comparisonResult = -1;
        break;
        //date02=date01
    case NSOrderedSame:
        comparisonResult = 0;
        break;
    default:
        NSLog(@"erorr dates %@, %@", date1, date2);
        break;
}
return comparisonResult;

} int comparisonResult = [self compareDate:startDate withDate:endDate]; if(comparisonResult >0){ //endDate 大 }

收藏
0
sina weixin mail 回到顶部