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 = @""; // 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 <= ) {
format = @"刚刚";
} else if(interval <= *){
format = [NSString stringWithFormat:@"发布于前%.f分钟", interval/];
} else if(interval <= **){
format = [NSString stringWithFormat:@"发布于前%.f小时", interval/];
} else if (interval <= ***){
format = [NSString stringWithFormat:@"发布于前%d天", (int)interval/(**)];
} else if (interval > *** & interval <= *** ){
format = [NSString stringWithFormat:@"发布于前%d周", (int)interval/(***)];
}else if(interval > *** ){
format = [NSString stringWithFormat:@"发布于前%d月", (int)interval/(***)];
} formatter.dateFormat = format;
return [formatter stringFromDate:date];
}
输出结果:
-- ::28.324 测试发布时间格式化[:] 发布于前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 < ) //1分钟
{
return @"最近";
}
else if(timeInterval < *) //1小时
{
return [NSString stringWithFormat:@"%d分钟前",(int)timeInterval/];
}
else if(timeInterval < **) //1天
{
return [NSString stringWithFormat:@"%d小时前",(int)timeInterval//];
} return [NSString stringWithFormat:@"%.1lf",timeInterval];
}
@end
输出结果:
-- ::28.325 测试发布时间格式化[:] 42分钟前
iOS: 格式化新浪微博/QQ说说等等的发布时间的更多相关文章
- iOS打开手机QQ与指定用户聊天界面
开发中遇到一个联系客服qq的需求,找到这么一个实现方法,先记录下来.大概的原理就是,iOS启动第三方应用是采用schema模式的,这有点像url,打开不同的界面使用不同的地址.但这个url怎么得来的还 ...
- iOS 格式化输出符号与类型转换
1.iOS 格式化输出符号 %@ 对象 %d, %i 整数 %u 无符号整形 %f 浮点(双字节) %x, %X 二进制整数 %o 八进制整数 %zi ...
- iOS 关于自动更新的分阶段发布(灰度发布)的相关简介
前言: AppStore 发布应用方式除了自动和手动,如今添加了分阶段发布(灰度发布).目的很明确,降低新版本骤然上升的bug率,不能挽回,只能发布新版本的风险.也也是针对禁止使用热修复,推出的相对 ...
- Android仿QQ ios dialog,仿QQ退出向上菜单
Android仿QQ ios dialog,仿QQ退出向上菜单 EasyDialog两种模式 仿QQ退出向上菜单,自己定义向上菜单 github地址:https://gith ...
- 2.1 -1.0 Xcode(发布时间、使用、快捷键、插件相关)
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 1.0 Xcode 发布时间 版本 iOS 版本 手机 日期 特殊介绍 Xcode 3.1 ...
- glibc 各版本发布时间以及内核默认glibc版本
最近有些软件要求glibc 2.14+,centos 6.x自带的版本是2.12的,特查了下glibc 各版本发布时间以及与对应的内核,如下: Complete glibc release histo ...
- iOS开发:保持程序在后台长时间运行
iOS开发:保持程序在后台长时间运行 2014 年 5 月 26 日 / NIVALXER / 0 COMMENTS iOS为了让设备尽量省电,减少不必要的开销,保持系统流畅,因而对后台机制采用墓碑式 ...
- 标题右边10px位置紧跟发布时间
一个ul列表,拥有若干li,内容是新闻标题,标题右边10px位置紧跟发布时间,当标题过长需要控制标题width,需要兼容ie6,不能用max-width h4{font-size:14px;heigh ...
- dede调用文章的栏目,作者,发布时间.以及连接数据库输出一个表
发布时间: {dede:field.pubdate function="MyDate('Y-m-d H:i',@me)"/}类目:{dede:field.typename/}发布人 ...
随机推荐
- 【洛谷 P4777】 【模板】扩展中国剩余定理(EXCRT)
注意一下:: 题目是 \[x≡b_i\pmod {a_i}\] 我总是习惯性的把a和b交换位置,调了好久没调出来,\(qwq\). 本题解是按照 \[x≡a_i\pmod {b_i}\] 讲述的,请注 ...
- Mac git
多次提交代码,与合并的工作量成反比. in terminal window git status:查看git的状态 git add -A: 把所有不再track里的文件加入进去/ git add -u ...
- Linux 格式化磁盘命令mkfs
linux格式化磁盘命令 mkfs 指令:mkfs 使用权限 : 超级使用者 使用方式 : mkfs [-V] [-t fstype] [fs-options] f ...
- mysql五-2:多表查询
一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 company.employeecompany.department #建表 create table department( id ...
- Linux终端彩色打印+终端进度条【转】
转自:https://my.oschina.net/jcseg/blog/178047 开发的一个应用程序选择了终端界面, 为了使软件稍微好看些, 研究下Linux终端的彩色打印, 并且基于这个彩色打 ...
- [转载]基于Redis的Bloomfilter去重(附Python代码)
前言: “去重”是日常工作中会经常用到的一项技能,在爬虫领域更是常用,并且规模一般都比较大.去重需要考虑两个点:去重的数据量.去重速度.为了保持较快的去重速度,一般选择在内存中进行去重. 数据量不大时 ...
- JS计算两个日期之间的天数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- BZOJ 3098
: Hash Killer II 时间限制: Sec 内存限制: MBSec Special Judge 提交: 解决: [提交][][] 题目描述 这天天气不错,hzhwcmhf神犇给VFleaKi ...
- processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(“ 132501”).active().singleResult();
JAVA: processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(“ 132501”).ac ...
- pipreqs------查找python项目依赖并生成requirements.txt
项目开发的时候,总是要搭建和部署环境,这时,就需要一个python第三方包的list,一般叫做requirements.txt.如果项目使用virtualenv环境,直接使用pip freeze即可, ...