iOS 计算当前时间与过去某一时间的时间差

//计算当前时间与订单生成时间的时间差,转化成分钟

-(NSInteger)dateTimeDifferenceWithStartTime:(NSString *)startTime

{

    NSDate *now = [NSDate date];

    

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

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];

    NSDate *startDate =[formatter dateFromString:startTime];

    

    NSString *nowstr = [formatter stringFromDate:now];

    NSDate *nowDate = [formatter dateFromString:nowstr];

    

    NSTimeInterval start = [startDate timeIntervalSince1970]*1;

    NSTimeInterval end = [nowDate timeIntervalSince1970]*1;

    NSTimeInterval value = end - start;

    

    int second = (int)value %60;//

    int minute = (int)value /60%60;

    int house = (int)value / (24 * 3600)%3600;

    int day = (int)value / (24 * 3600);

    NSString *str;

    NSInteger time;//剩余时间为多少分钟

    if (day != 0) {

        str = [NSString stringWithFormat:@"耗时%d%d小时%d%d",day,house,minute,second];

        time = day*24*60+house*60+minute;

    }else if (day==0 && house != 0) {

        str = [NSString stringWithFormat:@"耗时%d小时%d%d",house,minute,second];

        time = house*60+minute;

    }else if (day== 0 && house== 0 && minute!=0) {

        str = [NSString stringWithFormat:@"耗时%d%d",minute,second];

        time = minute;

    }else{

        str = [NSString stringWithFormat:@"耗时%d",second];

    }

    return time;

}


你可能感兴趣的:(时间)