iOS微博时间与微博来源文本的处理

-(NSString *)createdAt {
    // 取出数据结构为: Sat Apr 19 19:15:53 +0800 2014,将数据格式化输出业务数据
    NSDateFormatter *dfm = [[NSDateFormatter alloc] init];
    dfm.dateFormat = @"EEE MMM dd HH:mm:ss zzzz yyyy";
    dfm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

    // 格式取出的字符串,获取时间对象
    NSDate *createdTime = [dfm dateFromString:_createdAt];
    dfm.dateFormat = @"M月d日 HH点mm分";

    // 时间格式化成字符串
    NSString *createdTimeStr = [dfm stringFromDate:createdTime];

    NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:createdTime];
    NSTimeInterval second = time;       // 时间单位换算成 秒
    NSTimeInterval minute = time / 60;  // 时间单位换算成 分
    NSTimeInterval hour = minute / 60;  // 时间单位换算成 时
    NSTimeInterval day = hour / 24;     // 时间单位换算成 天
    NSTimeInterval year = day / 365;    // 时间单位换算成 年

    if (second < 60) {                  // 1分钟之内显示 "刚刚"
        return @"刚刚";
    } else if (minute < 60) {           // 1小时之内显示 "x分钟前"
        return [NSString stringWithFormat:@"%.f分钟前", minute];
    } else if (hour < 24) {             // 1天之内显示 "x小时前"
        return [NSString stringWithFormat:@"%.f小时前", hour];
    } else if (day < 7) {               // 1周之内显示 "x天前"
        return [NSString stringWithFormat:@"%.f天前", day];
    } else if (year >= 1) {             // 1年以前显示 "xxxx年x月x日"
        dfm.dateFormat = @"yyyy年M月d日";
        return [dfm stringFromDate:createdTime];
    } else {                            // 1年以内显示 "x月x日 x点x分"
        return createdTimeStr;
    } }



-(void)setSource:(NSString *)source
{
    // 源source结构为: <a href="http://app.weibo.com/t/feed/4ACxed" rel="nofollow">iPad客户端</a>
    NSInteger begin = [source rangeOfString:@">"].location + 1;
    NSInteger end = [source rangeOfString:@"</a>"].location;
    NSString *tempStr = [source substringWithRange:NSMakeRange(begin, end - begin)];

    // 从字符串取出"iPad客户端"再在前面拼接"来自"
    _source = [NSString stringWithFormat:@"来自%@", tempStr];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值