返回日期格式:2017-12-03T13:58:58.901Z,判断时间间隔 如 “刚刚”,“一分钟前”,“一小时前”等
后台返回的格式如下:
实现输出如下:
我的处理如下:
// 处理数据 2017-11-28T02:41:09.487Z
// 请求的时间戳。日期格式按照ISO8601标准表示,并需要使用UTC时间。
// 去掉.之后的字符串
NSArray *strArray = [time componentsSeparatedByString:@"."];
// 字符串转date
NSDate *registerDate = [NSString dateFromString:[NSString stringWithFormat:@"%@Z",strArray[]]];
// 对ISO8601标准时间date转String
NSString *str = [NSString timeStamp:registerDate];
// 对ISO8601标准时间String转时间间隔
NSString *str1 = [NSString JudgmentTimeIntervalWithISOTime:str];
self.registerTimeLabel.text = [NSString stringWithFormat:@"注册于:%@", [NSString compareCurrentTime:str1]];
具体的工具函数如下:
//方式一 后台给的格式为yyyy-MM-dd HH:mm:ss
+ (NSString *)compareCurrentTime:(NSString *)str {
//把字符串转为NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timeDate = [dateFormatter dateFromString:str];
NSDate *currentDate = [NSDate date];
//得到两个时间差
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
long temp = ;
NSString *result;
if (timeInterval/ < ){
result = [NSString stringWithFormat:@"刚刚"];
}
else if((temp = timeInterval/) <){
result = [NSString stringWithFormat:@"%ld分钟前",temp];
}
else if((temp = temp/) <){
result = [NSString stringWithFormat:@"%ld小时前",temp];
}
else if((temp = temp/) <){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
else if((temp = temp/) <){
result = [NSString stringWithFormat:@"%ld月前",temp];
} else{
temp = temp/;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
} //方式二 后台给的格式为 纯数字1352170595000(13位)
- (NSString *)updateTimeForRow:(NSString *)str {
// 获取当前时时间戳 1466386762.345715 十位整数 6位小数
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
// 创建歌曲时间戳(后台返回的时间 一般是13位数字)
NSTimeInterval createTime =[str floatValue]/;
// 时间差
NSTimeInterval time = currentTime - createTime; //秒转分钟
NSInteger small = time / ;
if (small == ) {
return [NSString stringWithFormat:@"刚刚"];
}
if (small < ) {
return [NSString stringWithFormat:@"%ld分钟前",small];
}
// 秒转小时
NSInteger hours = time/;
if (hours<) {
return [NSString stringWithFormat:@"%ld小时前",hours];
}
//秒转天数
NSInteger days = time//;
if (days < ) {
return [NSString stringWithFormat:@"%ld天前",days];
}
//秒转月
NSInteger months = time///;
if (months < ) {
return [NSString stringWithFormat:@"%ld月前",months];
}
//秒转年
NSInteger years = time////;
return [NSString stringWithFormat:@"%ld年前",years];
} // ISO8601的Date转String
+ (NSString *)timeStamp: (NSDate *)date {
// 获取当前时间
// NSDate *date = [NSDate new];
NSDateFormatter *timeFormatter = [NSDateFormatter new];
[timeFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[timeFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString *timestamp = [timeFormatter stringFromDate:date];
return timestamp;
} // String转Date
+ (NSDate *)dateFromString: (NSString *)str {
NSDateFormatter *timeFormatter = [NSDateFormatter new];
[timeFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[timeFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
return [timeFormatter dateFromString:str];
} // ISO8601格式字符串转Date
+ (NSDate *)dateFromISO8601String:(NSString *)string { if (!string) return nil;
struct tm tm;
time_t t;
strptime([string cStringUsingEncoding:NSUTF8StringEncoding], "%Y-%m-%dT%H:%M:%S%z", &tm);
tm.tm_isdst = -;
t = mktime(&tm);
// return [NSDate dateWithTimeIntervalSince1970:t]; // 零时区 return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];//东八区
}
//根据获取到的时间判断时间间隔 如 “刚刚”,“一分钟前”,“一小时前”等;
//获取时间 是用上面的方法获取的
+(NSString *)JudgmentTimeIntervalWithISOTime:(NSString *)timeStr{ NSDate *theDate = [self dateFromISO8601String:timeStr];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString * timeString = nil; NSTimeInterval cha = - [theDate timeIntervalSinceDate:[NSDate date]];
if (cha/ < ) {
timeString = [NSString stringWithFormat:@"%f", cha/];
timeString = [timeString substringToIndex:timeString.length-];
int num= [timeString intValue];
if (num <= ) {
timeString = [NSString stringWithFormat:@"刚刚"];
}else{
timeString = [NSString stringWithFormat:@"%@分钟前", timeString];
}
}
if (cha/ > && cha/ < ) {
timeString = [NSString stringWithFormat:@"%f", cha/];
timeString = [timeString substringToIndex:timeString.length-];
timeString = [NSString stringWithFormat:@"%@小时前", timeString];
}
if (cha/ > ){
timeString = [NSString stringWithFormat:@"%f", cha/];
timeString = [timeString substringToIndex:timeString.length-];
int num = [timeString intValue];
if (num < ) {
timeString = [NSString stringWithFormat:@"昨天"];
} else{
timeString =[dateFormatter stringFromDate:theDate];
}
}
return timeString;
}
以前没遇到过这个格式的日期,今天算是长见识了,哈哈。希望你遇到这日期能看到我的博客,哈哈
返回日期格式:2017-12-03T13:58:58.901Z,判断时间间隔 如 “刚刚”,“一分钟前”,“一小时前”等的更多相关文章
- SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断,格式化为YYYY-MM-DD
SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断: 在做sql数据的正确性审核中,需要判断数据是否满足日期格式,网上找不到相关的资料,于是自己花了半天写了一个简单的函数 具体 ...
- .Net Core WebApi返回日期格式的问题
环境:.net core 2.1 webapi 问题简介: 返回DateTime,前端接收到的字符有时候为2018-01-01T12:01:01,有时候为2018-01-01T01:01:01.722 ...
- js jq插件 显示中文时间戳 刚刚 N分钟前 N小时前 今天 上午 下午 日期格式化
注:页面需提前引用JQ ; $.fn.extend({ /* ** notes: 获取13位时间戳的简单操作 ** new Date('2018-02-01 15:10:00').getTime() ...
- java createSQLQuery().list()返回日期格式没有时分秒的解决方法
方法一 将Oracel数据库对应表中“收单时间的字段”receive_sheet_time,由原来的Date类型改为timestamp 然后,在java程序中,由 (java.util.timesta ...
- Json 返回日期格式转换
//日期转换 function ChangeDateFormat(time) { if (time != null) { var date = new Date(parseInt(time.repla ...
- springmvc 格式化返回日期格式
<mvc:annotation-driven conversion-service="conversionService"> <mvc:message-conve ...
- 用javascript写一个显示时间差 几分钟前 几小时前 几天前 几周前 大于一个月显示日期
window.onload = function(){ var show_times = $(".times span"); for(var i=0;i<show_times ...
- POI对Excel自定义日期格式的读取
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel private List<String[]> rosolveFile(InputStream is, String ...
- poi处理excel自定义日期格式
poi读取excel自定义时间类型时,读取到的是CELL_TYPE_NUMERIC,即数值类型,这个时候如果直接取值的话会发现取到的值和表格中的值不一样,这时应该先判断值是否是时间或者日期类型再进行处 ...
随机推荐
- SQL SERVER表变量和临时表
一.表变量 表变量在SQL Server 2000中首次被引入.表变量的具体定义包括列定义,列名,数据类型和约束.而在表变量中可以使用的约束包括主键约束,唯一约束,NULL约束和CHECK约束(外键约 ...
- java中的finally用法总结
不管 try 语句块正常结束还是异常结束,finally 语句块是保证要执行的.如果 try 语句块正常结束,那么在 try 语句块中的语句都执行完之后,再执行 finally 语句块.如果 try ...
- C# TreeView 右键菜单
方法一: 在winform中,添加一个contextMenuStrip1,设置TreeView的属性ContextMenuStrip为contextMenuStrip1,并为这个contextMenu ...
- IntelliJ IDEA使用教程一 介绍&安装&配置
http://blog.csdn.net/nextyu/article/details/47206015 全套
- 如何用Xshell导出文件到桌面本地
在软件开发中,会经常用到登录到Linux服务器,查看相关日志,同时也会远程取出文件到本地环境, 在没有xftp客户端的情况下,如何直接使用xshell软件直接下载文件到本地呢 下载文件: 使用sz命令 ...
- P3975 (后缀自动机sort)
题目链接: https://www.luogu.org/problem/P3975 题意: 求出所有字串的第k大子串 有两种,第一种对于出现在不同位置的相同子串算作一个子串 第二种,对于不同位置的子串 ...
- C++标准库分析总结(三)——<迭代器设计原则>
本节主要总结迭代器的设计原则,以及iterstor traits的设计作用 1.迭代器遵循的原则 迭代器是算法和容器的桥梁,它是类模板的设计,迭代器必须有能力回答算法提出的问题才能去搭配该算法的使用 ...
- spark错误记录总结
1.执行spark-submit时出错 执行任务如下: # ./spark-submit --class org.apache.spark.examples.SparkPi /hadoop/spark ...
- Linux 如何通过某一台服务器调用执行多台远程服务器上的脚本,结果显示在本地?
现在都流行自动化运维了,可能目前技术不够,很多自动化工具还不怎么会用,所以本次只是通过ssh来实现功能. 说明:自己写的一个简单脚本,只是实现了基础功能,还有待优化. 一共三台机器: master:1 ...
- C#读写西门子PLC数据
C#读写西门子PLC数据,包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC 本文将使用一个gitHub开源的组件技术来读写西门子plc数 ...