使用字符串比较两个时间的大小


- (NSString *)compareStr1:(NSString *)str1 withStr2:(NSString *)str2{

    NSString *hour = nil;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];

    NSDate *date1 = [formatter dateFromString:str1];
    NSDate *data2 = [formatter dateFromString:str2];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    unsigned int usitFlags = NSCalendarUnitMinute;

    NSDateComponents *coms = [gregorian components:usitFlags fromDate:date1 toDate:data2 options:0];

    NSInteger minute = [coms minute];
    NSLog(@"minute- - - - -%ld",(long)minute);

    CGFloat hour1 = minute / 60;

    NSInteger hour2 = minute % 60;
    if (hour2 > 30) {
        hour1 = hour1 + 1;
    }
    else if (hour2 > 0 && hour2 <= 30){
        hour1 = hour1 + 0.5;
    }
    hour = [NSString stringWithFormat:@"%.1f",hour1];

    return hour;
}

你可能感兴趣的:(iOS--O-C语言,技术研究)