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/}发布人 ...
随机推荐
- HDU2441 ACM(Array Complicated Manipulation)
ACM(Array Complicated Manipulation) Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- android ARM 汇编学习—— 在 android 设备上编译c/cpp代码并用objdump/readelf等工具分析
学习 android 逆向分析过程中,需要学习 Arm 指令,不可避免要编写一些 test code 并分析其指令,这是这篇文档的背景. 在目前 android 提供的开发环境里,如果要编写 c / ...
- CentOS RabbitMQ安装
1. 安装erlang,参考另外一篇文章: http://www.cnblogs.com/wanpengcoder/p/5287302.html 2. 安装libxslt和: yum install ...
- 虚拟机vmware10.0.0里设置Suse Linux Enterprise 11系统静态IP上网
http://blog.csdn.net/usbdrivers/article/details/50035615 首次在虚拟机里安装Suse Linux Enterprise 11,采用NET方式能够 ...
- python进程池pool的starmap的使用
#!/usr/bin/env python3 from functools import partial from itertools import repeat from multiprocessi ...
- 出现“error c4430缺少类型说明符-假定为int。注意C++不支持默认int
出现这种错误的原因,是因为函数没有写返回值.是在VC6.0的工程转为高版本(VS2010)的时候经常出现的; #include <stdio.h> main() { printf(&quo ...
- Laravel 中的 Many-To-Many
在实际的开发中,我们经常会接触到几种常见的对应关系模式: One-To-One //一对一 One-To-Many //一对多 Many-To-Many //多对多 在刚刚开始接触到这些概念的时候,其 ...
- mysql TIMESTAMPDIFF
在MySQL应用时,经常要使用这两个函数TIMESTAMPDIFF和TIMESTAMPADD. 一,TIMESTAMPDIFF 语法: TIMESTAMPDIFF(interval,datetime_ ...
- 发布Office 365插件
在上一篇博客<VisualStudio 2013开发Office插件>开发完成了插件后,需要将插件发布 发布前需要: Azure 应用服务,作为Office插件的发布空间,地址是:http ...
- Python与数据结构[3] -> 树/Tree[2] -> AVL 平衡树和树旋转的 Python 实现
AVL 平衡树和树旋转 目录 AVL平衡二叉树 树旋转 代码实现 1 AVL平衡二叉树 AVL(Adelson-Velskii & Landis)树是一种带有平衡条件的二叉树,一棵AVL树其实 ...