返回的師NSString類型
//獲取時(shí)間----有--時(shí)分秒
long long timeString= [time longLongValue];
//獲取時(shí)間----有--時(shí)分秒
//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];
//獲取時(shí)間----只有日期的時(shí)間
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"YYYY-MM-dd HH:mm"];
NSArray *timearray1 = [myTime componentsSeparatedByString:@"-"];
//獲得系統(tǒng)時(shí)間
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;
//與現(xiàn)在的時(shí)間對(duì)比判斷
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小時(shí)前", a2-a1];
}
return [NSString stringWithFormat:@"%d小時(shí)前", a2-a1];
}
return @"剛剛";
}
}return nil;