首页 > 代码库 > ios-时间换算

ios-时间换算

经常会遇到时间转换的,在此收藏一个时间换算的方法?

 1 #pragma mark 时间换算 2  3 + (NSString *)setcreateTime:(NSString *)str 4 { 5     //yyyy-MM-dd- HH:MMss"]; 6  7      8     NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; 9     10     //[formatter setDateStyle:NSDateFormatterMediumStyle];11     12     //[formatter setTimeStyle:NSDateFormatterShortStyle];13     14     [formatter setDateFormat:@"yyyy"];15     16     [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CCT"]];17     18     NSDate *date = [NSDate dateWithTimeIntervalSince1970:[str doubleValue]];19     20     NSString *dateStr = [formatter stringFromDate:date];21     22     //本地时间23     NSDate * senddate=[NSDate date];24     NSDateFormatter * dateformatter=[[NSDateFormatter alloc] init];25     [dateformatter setDateFormat:@"YYYY"];26     NSString * locationString=[dateformatter stringFromDate:senddate];27  28     //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];29     30     //判断创建时间 与 本地时间 是否同一年31     if ([dateStr isEqualToString:locationString]) {32         [formatter setDateFormat:@"MM-dd HH:mm"];33     }else{34         [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];35     }36     37     date = [NSDate dateWithTimeIntervalSince1970:[str doubleValue]];38     dateStr = [formatter stringFromDate:date];39     40     return [self compareCurrentTime:date :dateStr];41 }42 43 + (NSString *) compareCurrentTime:(NSDate*) compareDate :(NSString *)str44 {45     NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];46     timeInterval = -timeInterval;47     long temp = 0;48     NSString *result;49     if (timeInterval < 60) {50         if (timeInterval > 1) {51             result = [NSString stringWithFormat:@"%d秒前",(int)timeInterval+1];52         }else if (timeInterval < -160){53             result = str;54         }55         else{56             result = [NSString stringWithFormat:@"%d秒前",1];57         }58         59     }60     else if((temp = timeInterval/60) <60){61         result = [NSString stringWithFormat:@"%ld分钟前",temp];62     }else{63         result = str;64     }65     66 67     68     return  result;69 }
View Code

 

ios-时间换算