美文网首页
IOS 判断某个时间距离现在已经过了多久

IOS 判断某个时间距离现在已经过了多久

作者: Spring_Lau | 来源:发表于2016-07-15 16:45 被阅读277次

+(NSString *)getUTCFormateDate:(NSString *)newsDate

{

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

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

NSLog(@"newsDate = %@",newsDate);

NSDate *newsDateFormatted = [dateFormatter dateFromString:newsDate];

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];

[dateFormatter setTimeZone:timeZone];

NSDate* current_date = [[NSDate alloc] init];

NSTimeInterval time=[current_date timeIntervalSinceDate:newsDateFormatted];//间隔的秒数

int year =((int)time)/(3600*24*30*12);

int month=((int)time)/(3600*24*30);

int days=((int)time)/(3600*24);

int hours=((int)time)%(3600*24)/3600;

int minute=((int)time)%(3600*24)/60;

NSLog(@"time=%f",(double)time);

NSString *dateContent;

if (year!=0) {

dateContent = newsDate;

}else if(month!=0){

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",month,@"个月前"];

}else if(days!=0){

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",days,@"天前"];

}else if(hours!=0){

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",hours,@"小时前"];

}else {

dateContent = [NSString stringWithFormat:@"%@%i%@",@"  ",minute,@"分钟前"];

}

return dateContent;

}

相关文章

网友评论

      本文标题:IOS 判断某个时间距离现在已经过了多久

      本文链接:https://www.haomeiwen.com/subject/xyvyjttx.html