返回的师NSString类型
//获取时间----有--时分秒
long long timeString= [time longLongValue];
//获取时间----有--时分秒
//long long oldtime= [timeString longLongValue];
NSDate *d = [NSDate dateWithTimeIntervalSince1970:timeString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
NSString *myTime = [formatter stringFromDate:d];
//获取时间----只有日期的时间
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"YYYY-MM-dd HH:mm"];
NSArray *timearray1 = [myTime componentsSeparatedByString:@"-"];
//获得系统时间
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
NSString * systemtime=[dateformatter stringFromDate:senddate];
//DLog(@"dateformatter---%@",systemtime);
NSArray *timearray2 = [systemtime componentsSeparatedByString:@"-"];
// NSString *time =nil;
//与现在的时间对比判断
if ([timearray1 count] == 6 && [timearray2 count] == 6) {
if (!([timearray1[0] intValue] == [timearray2[0] intValue]) || !([timearray1[1] intValue] == [timearray2[1] intValue]) || !([timearray1[2] intValue] == [timearray2[2] intValue])) {
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:@"MM-dd HH:mm"];
NSString *myTime2 = [formatter2 stringFromDate:d];
return myTime2;
}
if ([timearray1[3] intValue] == [timearray2[3] intValue]) {
if (timearray1[4] == timearray2[4]) {
return ([timearray2[5] intValue] - [timearray1[5] intValue] > 0) ? [NSString stringWithFormat:@"%d秒前", [timearray2[5] intValue] - [timearray1[5] intValue]] : @"刚刚";
}
return ([timearray2[4] intValue] - [timearray1[4] intValue] > 0) ? [NSString stringWithFormat:@"%d分钟前", [timearray2[4] intValue] - [timearray1[4] intValue]] : @"刚刚";
}
else {
int a1 = [timearray1[3] intValue];
int a2 = [timearray2[3] intValue];
if (a2-a1 > 0) {
if (a2-a1 == 1) {
return ([timearray1[4] intValue] - [timearray2[4] intValue] > 0) ? [NSString stringWithFormat:@"%d分钟前", 60 - [timearray1[4] intValue] + [timearray2[4] intValue]] : [NSString stringWithFormat:@"%d小时前", a2-a1];
}
return [NSString stringWithFormat:@"%d小时前", a2-a1];
}
return @"刚刚";
}
}return nil;