iOS常用于显示几小时前/几天前/几月前/几年前的代码片段

iOS常用于显示几小时前/几天前/几月前/几年前的代码片段

版权声明:请关注个人博客:http://www.henishuo.com/

 
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Retain a formated string with a real date string 
  3.  * 
  4.  * @param dateString a real date string, which can be converted to a NSDate object 
  5.  * 
  6.  * @return a string that will be x分钟前/x小时前/昨天/x天前/x个月前/x年前 
  7.  */  
  8. + (NSString *)timeInfoWithDateString:(NSString *)dateString {  
  9.   // 把日期字符串格式化为日期对象  
  10.   NSDate *date = [NSDate dateFromString:dateString withFormat:@"yyyy-MM-dd HH:mm:ss"];  
  11.     
  12.   NSDate *curDate = [NSDate date];  
  13.   NSTimeInterval time = -[date timeIntervalSinceDate:curDate];  
  14.     
  15.   int month = (int)([curDate getMonth] - [date getMonth]);  
  16.   int year = (int)([curDate getYear] - [date getYear]);  
  17.   int day = (int)([curDate getDay] - [date getDay]);  
  18.     
  19.   NSTimeInterval retTime = 1.0;  
  20.   // 小于一小时  
  21.   if (time < 3600) {  
  22.     retTime = time / 60;  
  23.     retTime = retTime <= 0.0 ? 1.0 : retTime;  
  24.     return [NSString stringWithFormat:@"%.0f分钟前", retTime];  
  25.   }  
  26.   // 小于一天,也就是今天  
  27.   else if (time < 33600 * 24) {  
  28.     retTime = time / 3600;  
  29.     retTime = retTime <= 0.0 ? 1.0 : retTime;  
  30.     return [NSString stringWithFormat:@"%.0f小时前", retTime];  
  31.   }  
  32.   // 昨天  
  33.   else if (time < 33600 * 224 * 2) {  
  34.     return @"昨天";  
  35.   }  
  36.   // 第一个条件是同年,且相隔时间在一个月内  
  37.   // 第二个条件是隔年,对于隔年,只能是去年12月与今年1月这种情况  
  38.   else if ((abs(year) == 0 && abs(month) <= 1)  
  39.            || (abs(year) == 1 && [curDate getMonth] == 1 && [date getMonth] == 12)) {  
  40.     int retDay = 0;  
  41.     // 同年  
  42.     if (year == 0) {  
  43.       // 同月  
  44.       if (month == 0) {  
  45.         retDay = day;  
  46.       }  
  47.     }  
  48.       
  49.     if (retDay <= 0) {  
  50.       // 这里按月最大值来计算  
  51.       // 获取发布日期中,该月总共有多少天  
  52.       int totalDays = [NSDate daysInMonth:(int)[date getMonth] year:(int)[date getYear]];  
  53.       // 当前天数 + (发布日期月中的总天数-发布日期月中发布日,即等于距离今天的天数)  
  54.       retDay = (int)[curDate getDay] + (totalDays - (int)[date getDay]);  
  55.         
  56.       if (retDay >= totalDays) {  
  57.         return [NSString stringWithFormat:@"%d个月前", (abs)(MAX(retDay / 31, 1))];  
  58.       }  
  59.     }  
  60.       
  61.     return [NSString stringWithFormat:@"%d天前", (abs)(retDay)];  
  62.   } else  {  
  63.     if (abs(year) <= 1) {  
  64.       if (year == 0) { // 同年  
  65.         return [NSString stringWithFormat:@"%d个月前", abs(month)];  
  66.       }  
  67.         
  68.       // 相差一年  
  69.       int month = (int)[curDate getMonth];  
  70.       int preMonth = (int)[date getMonth];  
  71.         
  72.       // 隔年,但同月,就作为满一年来计算  
  73.       if (month == 12 && preMonth == 12) {  
  74.         return @"1年前";  
  75.       }  
  76.         
  77.       // 也不看,但非同月  
  78.       return [NSString stringWithFormat:@"%d个月前", (abs)(12 - preMonth + month)];  
  79.     }  
  80.       
  81.     return [NSString stringWithFormat:@"%d年前", abs(year)];  
  82.   }  
  83.     
  84.   return @"1小时前";  
  85. }  

这里计算多少个月前时,为了减少计算量,没有分别获取对应月份的总天数,而是使用月份最大值31作为标准,因此,

如果需要更精准的计算,把对应的一小段代码替换掉即可

posted @   文化流氓  阅读(413)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示