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分钟前

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5160381.html ,如需转载请自行联系原作者
优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/363438

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。