NSString *distanceTime = [self returnFromTheTimeOfToday:@"2016-09-29 01:45:10"];
#pragma mark 默认一分钟有60秒,一小时就60分钟 一天有24小时,一周有7天,一个月有30天,一年有12个月 不考虑其他的如平年2月28天,闰月29天这些情况
- (NSString *)returnFromTheTimeOfToday:(NSString *)timeStr
{
//timeStr 字符串格式->2016-09-29 01:45:10
NSDateFormatter *form = [[NSDateFormatter alloc] init];
//设置时区
form.locale = [NSLocale localeWithLocaleIdentifier:@"cn"];
form.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [form dateFromString:timeStr];
//得到当前的时间差
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
timeInterval = -timeInterval;
//然后进行时间的比较
if(timeInterval < 60)
{
return [NSString stringWithFormat:@"刚刚"];
}
//分钟
NSInteger minute = timeInterval / 60;
if(minute < 60)
{
return [NSString stringWithFormat:@"%ld分钟之前",minute];
}
NSInteger hours = minute / 60;
if(hours < 24)
{
return [NSString stringWithFormat:@"%ld小时之前",hours];
}
NSInteger day = hours / 24;
NSInteger month = day/30;
NSInteger year = month/12;
if(day <= 1)
{
form.dateFormat = @"HH:mm";
NSString *oldtime = [form stringFromDate:date];
return [NSString stringWithFormat:@"昨天 %@",oldtime];
}
if(day < 7)
{
return [NSString stringWithFormat:@"%ld天之前",day];
}
if((day/7)<4)
{
return [NSString stringWithFormat:@"%ld周之前",day/7];
}
else if(month < 12)
{
return [NSString stringWithFormat:@"%ld月之前",month];
}
else
{
form.dateFormat = @"yyyy-MM-dd HH:mm";
return [NSString stringWithFormat:@"%ld年之前",year];
}
return nil;
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。