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/}发布人 ...
随机推荐
- usaco 月赛 2005 january watchcow
2013-09-18 08:13 //By BLADEVIL var n, m :longint; pre, other :..] of longint; last :..] of longint; ...
- Codeforces 940F Machine Learning 带修改莫队
题目链接 题意 给定一个长度为\(n\)的数组\(a\),\(q\)个操作,操作分两种: 对于区间\([l,r]\),询问\(Mex\{c_0,c_1,c_2,⋯,c_{10^9}\}\),其中\(c ...
- LeetCode 3 :Min Stack
今天做了一道MinStack的题,深深的感到自己C++还完全没有学好!!! #include <iostream> #include <stack> using std::st ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- android 调试 native 程序的方法
一.背景 首先说需求,这个需求非常常见,就是android上需要的一个功能,linux已经有开源代码而且非常稳定,希望能直接porting过去使用,这个程序是pure c 的代码,也就是说,跟andr ...
- Shell中的while循环【转】
转自:http://blog.chinaunix.net/uid-25880122-id-2901409.html while循环的格式 while expression do command c ...
- oracle创建用户赋予权限,删除权限
--删除用户及及用户下的所有数据 drop user xxx cascade; --创建用户赋予密码 ; --赋予权限 grant dba to xxx; --删除权限 revoke dba from ...
- centos 7 卸载自带的jdk
# 查看jdk安装信息 rpm -qa|grep java 卸载已安装的jdk: # yum -y remove java java-1.7.0-*
- WCF发布方式介绍
转载出处:http://blog.csdn.net/fangxing80/article/details/6101790 从VS2005推出WCF以来,WCF逐步取代了Remoting, WebSer ...
- node中通过orm2链接mysql的一个坑
代码是orm上的例子,出现如下错误: ORMError: Connection protocol not supported - have you installed the database dri ...