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

简介:

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

是NSDate的一个扩展方法:

/**
 * Given the reference date and return a pretty date string to show
 *
 * @param refrence the date to refrence
 *
 * @return a pretty date string, like "just now", "1 minute ago", "2 weeks ago", etc
 */
- (NSString *)prettyDateWithReference:(NSDate *)reference {
  NSString *suffix = @"ago";
  
  float different = [reference timeIntervalSinceDate:self];
  if (different < 0) {
    different = -different;
    suffix = @"from now";
  }
  
  // days = different / (24 * 60 * 60), take the floor value
  float dayDifferent = floor(different / 86400);
  
  int days   = (int)dayDifferent;
  int weeks  = (int)ceil(dayDifferent / 7);
  int months = (int)ceil(dayDifferent / 30);
  int years  = (int)ceil(dayDifferent / 365);
  
  // It belongs to today
  if (dayDifferent <= 0) {
    // lower than 60 seconds
    if (different < 60) {
      return @"just now";
    }
    
    // lower than 120 seconds => one minute and lower than 60 seconds
    if (different < 120) {
      return [NSString stringWithFormat:@"1 minute %@", suffix];
    }
    
    // lower than 60 minutes
    if (different < 60 * 60) {
      return [NSString stringWithFormat:@"%d minutes %@", (int)floor(different / 60), suffix];
    }
    
    // lower than 60 * 2 minutes => one hour and lower than 60 minutes
    if (different < 7200) {
      return [NSString stringWithFormat:@"1 hour %@", suffix];
    }
    
    // lower than one day
    if (different < 86400) {
      return [NSString stringWithFormat:@"%d hours %@", (int)floor(different / 3600), suffix];
    }
  }
  // lower than one week
  else if (days < 7) {
    return [NSString stringWithFormat:@"%d day%@ %@", days, days == 1 ? @"" : @"s", suffix];
  }
  // lager than one week but lower than a month
  else if (weeks < 4) {
    return [NSString stringWithFormat:@"%d week%@ %@", weeks, weeks == 1 ? @"" : @"s", suffix];
  }
  // lager than a month and lower than a year
  else if (months < 12) {
    return [NSString stringWithFormat:@"%d month%@ %@", months, months == 1 ? @"" : @"s", suffix];
  }
  // lager than a year
  else {
    return [NSString stringWithFormat:@"%d year%@ %@", years, years == 1 ? @"" : @"s", suffix];
  }
  
  return self.description;
}


目录
相关文章
|
10月前
|
Android开发 iOS开发
iOS 一个类似安卓的日期选择器
在iOS的app上一般的日期选择器都会选择用 UIDatePicker 来做,但是我之前的产品经理并不喜欢那种选择的模式。说事要统一安卓和iOS的设计风格。这就是我写这个选择器的初衷。
|
iOS开发
iOS开发 - ScrollView滚动时怎么判断滚动停止及滚动的方向
iOS开发 - ScrollView滚动时怎么判断滚动停止及滚动的方向
827 0
|
iOS开发
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
120 0
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
|
iOS开发
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
236 0
iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上
|
数据安全/隐私保护 iOS开发
iOS逆向小技能:解锁无密码的设备、判断设备是否锁定、锁定设备、打开某个程序
介绍lua 函数: runApp、closeApp、getScreenSize、getDeviceID、lua_exit、isFrontApp。
195 0
|
iOS开发
IOS常用正则表达式判断
IOS常用正则表达式判断
62 0
|
JavaScript iOS开发
JS判断IOS系统版本
JS判断IOS系统版本
324 0
|
iOS开发
IOS判断网络连接
IOS判断网络连接
199 0
|
JavaScript 前端开发 Android开发
根据js来判断手机是操作系安卓还是ios
根据js来判断手机是操作系安卓还是ios
530 0
|
Android开发 iOS开发
判断是否为安卓或者IOS
判断是否为安卓或者IOS
443 0