iOS: 格式化新浪微博/QQ说说等等的发布时间

介绍:对于一些社交工具,我们可以发布一些说说或者心情什么的,如新浪微博,QQ,微信等,发布成功后,上面都会有一个发布的时间。

这个时间并不是具体的NSDate类型,而是经过格式化过的符合一般标准的模式,例如:发布于前一个月、前一个星期、前一天、十几分钟前、刚刚等。

下面就给出两个具体的测试Demo

头文件:

//  ViewController.m
//  测试发布时间格式化
//
//  Created by mac on 16/1/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"

#define knewsTimeFormat @"yyyyMMddHHmmss" //你要传过来日期的格式

#define kcreatedDateFormat @"EEE MMM dd HH:mm:ss Z yyyy" //你要传过来日期的格式

#define kLocaleIdentifier @"en_US"  //时区类型

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     注意:传入的需要格式化的时间字符串必须与你设置的日期的格式对应
//测试一
    NSString *str = @"20160126132609";  //  2016/01/26 13:26:09
    NSLog(@"%@",[self newsTime:str]);
    
    
    //测试二
    NSString *str2 = @"Tue Jan 26 13:50:08 +0800 2016";
    NSLog(@"%@",[self formatCreatedDate:str2]);
}

测试一:

//方式一: 获取发布时间
- (NSString *)newsTime:(NSString *)newsTimes
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = knewsTimeFormat;
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:kLocaleIdentifier];
    
    NSDate *date = [formatter dateFromString:newsTimes];
    
    NSDate *now = [NSDate date];
    
    // 比较帖子发布时间和当前时间
    NSTimeInterval interval = [now timeIntervalSinceDate:date];
    
    NSString *format;
    if (interval <= 60) {
        format = @"刚刚";
    } else if(interval <= 60*60){
        format = [NSString stringWithFormat:@"发布于前%.f分钟", interval/60];
    } else if(interval <= 60*60*24){
        format = [NSString stringWithFormat:@"发布于前%.f小时", interval/3600];
    } else if (interval <= 60*60*24*7){
        format = [NSString stringWithFormat:@"发布于前%d天", (int)interval/(60*60*24)];
    } else if (interval > 60*60*24*7 & interval <= 60*60*24*30 ){
        format = [NSString stringWithFormat:@"发布于前%d周", (int)interval/(60*60*24*7)];
    }else if(interval > 60*60*24*30 ){
        format = [NSString stringWithFormat:@"发布于前%d月", (int)interval/(60*60*24*30)];
    }
    
    formatter.dateFormat = format;
    return [formatter stringFromDate:date];
}

输出结果:

2016-01-26 14:32:28.324 测试发布时间格式化[1809:109947] 发布于前1小时

 

 

 

测试二:

//方式二: 获取发布时间
-(NSString *)formatCreatedDate:(NSString *)newsTimes
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    formatter.dateFormat = kcreatedDateFormat;
    
    formatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:kLocaleIdentifier];
    
    NSDate *date = [formatter dateFromString:newsTimes];
    
    NSDate *now = [NSDate date];
    
    // 比较帖子发布时间和当前时间
    NSTimeInterval timeInterval =  [now timeIntervalSinceDate:date];
    
    if(timeInterval < 60) //1分钟
    {
        return @"最近";
    }
    else if(timeInterval < 60*60)  //1小时
    {
        return [NSString stringWithFormat:@"%d分钟前",(int)timeInterval/60];
    }
    else if(timeInterval < 60*60*24) //1天
    {
        return [NSString stringWithFormat:@"%d小时前",(int)timeInterval/60/60];
    }
    
    return [NSString stringWithFormat:@"%.1lf",timeInterval];
}
@end

输出结果:

2016-01-26 14:32:28.325 测试发布时间格式化[1809:109947] 42分钟前

 

posted @ 2016-01-26 14:46  XYQ全哥  阅读(341)  评论(0编辑  收藏  举报