您的位置:首页 > 编程语言

处理时间格式的代码处理

2016-01-12 10:30 218 查看
-(NSString *) FormartTime:(NSDate*) compareDate
{
if( compareDate == nil ) return @"";

NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
timeInterval = -timeInterval;
long temp = timeInterval;
NSString *result;

if (timeInterval < 60) {
if( temp == 0 )
result = @"刚刚";
else
result = [NSString stringWithFormat:@"%d秒前",(int)temp];
}
else if(( timeInterval/60) <60){
result = [NSString stringWithFormat:@"%d分钟前",(int)temp/60];
}

else if(( temp/86400) <30){

NSDateFormatter *date = [[NSDateFormatter alloc] init];
[date setDateFormat:@"dd"];
NSString *str = [date stringFromDate:[NSDate date]];
int nowday = [str intValue];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString *strDate = [dateFormatter stringFromDate:compareDate];
int day = [strDate intValue];
if (nowday-day==0) {
//[dateFormatter setDateFormat:@"今天 HH:mm"];
[dateFormatter setDateFormat:@"HH:mm"];
result =    [dateFormatter stringFromDate:compareDate];

}
else if(nowday-day==1)
{

[dateFormatter setDateFormat:@"昨天 HH:mm"];
// result =  [dateFormatter stringFromDate:compareDate];
result =  @"昨天";

}

else if( temp < 8 )
{
if (temp==1) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"昨天HH:mm"];
NSString *strDate = [dateFormatter stringFromDate:compareDate];
result = strDate;
}
else if(temp == 2)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"前天HH:mm"];
NSString *strDate = [dateFormatter stringFromDate:compareDate];
result = strDate;
}

}
else
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd HH:mm"];
NSString *strDate = [dateFormatter stringFromDate:compareDate];
result = strDate;

}
}
else
{//超过一个月的就直接显示时间了
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *strDate = [dateFormatter stringFromDate:compareDate];
result = strDate;
}

/*
else if((temp = (temp/(3600*24))/30) <12){
result = [NSString stringWithFormat:@"%d个月前",(int)temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%d年前",(int)temp];
}
*/

return  result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: