返回日期格式: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,即数值类型,这个时候如果直接取值的话会发现取到的值和表格中的值不一样,这时应该先判断值是否是时间或者日期类型再进行处 ...
随机推荐
- vue2 路由,运动
- mysql中一个字段升序,另一个字段降序
mySql中,升序为asc,降序为desc.例如: 升序:select * from 表名 order by 表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select ...
- JS闭包是什么?
闭包是js开发惯用的技巧,什么是闭包? 闭包指的是:能够访问另一个函数作用域的变量的函数. 清晰的讲:闭包就是一个函数,这个函数能够访问其他函数的作用域中的变量. function outer(){ ...
- [Angular] Show a Loading Indicator for Lazy Routes in Angular
We can easily code split and lazy load a route in Angular. However when the user then clicks that la ...
- HashMap与HashTable的理解与区别
Hashtable是java一开始发布时就提供的键值映射的数据结构,而HashMap产生于JDK1.2.虽然Hashtable比HashMap出现的早一些,但是现在Hashtable基本上已经被弃用了 ...
- Java知识点汇总-2
目录 1 变量的作用域 2 二维数组的定义 1 变量的作用域 实例代码: public void fight(String name){ if ("Bean".equals(nam ...
- 【安卓笔记】Android接入https证书进行请求
Nginx在开发环境用的还是比较少,之前用在Web开发中解决跨域的问题,在安卓开发中如果想经过Nginx开启https并且转发到其他服务器,相关的配置步骤也不是特别复杂. Android使用自签名证书 ...
- Yarn 配置阿里源
1.查看一下当前源 yarn config get registry 2.切换为淘宝源 yarn config set registry https://registry.npm.taobao.org ...
- Ubuntu下Django+uWSGI+nginx部署
本文采用uwsgi+nginx来部署django 这种方式是将nginx作为服务端前端,将接受web所有的请求,统一管理,Nginx把所有的静态请求自己处理,然后把所有非静态请求通过uwsgi传递给D ...
- TensorFlow(十三):模型的保存与载入
一:保存 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist ...