打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS根据日期判断是刚刚、几分钟前、几小时前等的代码片段

参考github源码中的一个格式化字符串的源码,

是NSDate的一个扩展方法:

  1. /** 
  2.  * Given the reference date and return a pretty date string to show 
  3.  * 
  4.  * @param refrence the date to refrence 
  5.  * 
  6.  * @return a pretty date string, like "just now", "1 minute ago", "2 weeks ago", etc 
  7.  */  
  8. - (NSString *)prettyDateWithReference:(NSDate *)reference {  
  9.   NSString *suffix = @"ago";  
  10.     
  11.   float different = [reference timeIntervalSinceDate:self];  
  12.   if (different < 0) {  
  13.     different = -different;  
  14.     suffix = @"from now";  
  15.   }  
  16.     
  17.   // days = different / (24 * 60 * 60), take the floor value  
  18.   float dayDifferent = floor(different / 86400);  
  19.     
  20.   int days   = (int)dayDifferent;  
  21.   int weeks  = (int)ceil(dayDifferent / 7);  
  22.   int months = (int)ceil(dayDifferent / 30);  
  23.   int years  = (int)ceil(dayDifferent / 365);  
  24.     
  25.   // It belongs to today  
  26.   if (dayDifferent <= 0) {  
  27.     // lower than 60 seconds  
  28.     if (different < 60) {  
  29.       return @"just now";  
  30.     }  
  31.       
  32.     // lower than 120 seconds => one minute and lower than 60 seconds  
  33.     if (different < 120) {  
  34.       return [NSString stringWithFormat:@"1 minute %@", suffix];  
  35.     }  
  36.       
  37.     // lower than 60 minutes  
  38.     if (different < 660 * 60) {  
  39.       return [NSString stringWithFormat:@"%d minutes %@", (int)floor(different / 60), suffix];  
  40.     }  
  41.       
  42.     // lower than 60 * 2 minutes => one hour and lower than 60 minutes  
  43.     if (different < 7200) {  
  44.       return [NSString stringWithFormat:@"1 hour %@", suffix];  
  45.     }  
  46.       
  47.     // lower than one day  
  48.     if (different < 86400) {  
  49.       return [NSString stringWithFormat:@"%d hours %@", (int)floor(different / 3600), suffix];  
  50.     }  
  51.   }  
  52.   // lower than one week  
  53.   else if (days < 7) {  
  54.     return [NSString stringWithFormat:@"%d day%@ %@", days, days == 1 ? @"" : @"s", suffix];  
  55.   }  
  56.   // lager than one week but lower than a month  
  57.   else if (weeks < 4) {  
  58.     return [NSString stringWithFormat:@"%d week%@ %@", weeks, weeks == 1 ? @"" : @"s", suffix];  
  59.   }  
  60.   // lager than a month and lower than a year  
  61.   else if (months < 12) {  
  62.     return [NSString stringWithFormat:@"%d month%@ %@", months, months == 1 ? @"" : @"s", suffix];  
  63.   }  
  64.   // lager than a year  
  65.   else {  
  66.     return [NSString stringWithFormat:@"%d year%@ %@", years, years == 1 ? @"" : @"s", suffix];  
  67.   }  
  68.     
  69.   return self.description;  
  70. }  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
NSString与int和float的相互转换
iOS---------金额转大写
OC 常用数据类型之间的转换
数据库 ios
C语言编程 有一篇文章,共有3行文字,每行80个字符。要求分别统计出其中英文字母,数字,空...
Java数字转中文大写,数字转英文
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服