后台返回的格式如下:

实现输出如下:

我的处理如下:

// 处理数据 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,判断时间间隔 如 “刚刚”,“一分钟前”,“一小时前”等的更多相关文章

  1. SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断,格式化为YYYY-MM-DD

    SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断: 在做sql数据的正确性审核中,需要判断数据是否满足日期格式,网上找不到相关的资料,于是自己花了半天写了一个简单的函数 具体 ...

  2. .Net Core WebApi返回日期格式的问题

    环境:.net core 2.1 webapi 问题简介: 返回DateTime,前端接收到的字符有时候为2018-01-01T12:01:01,有时候为2018-01-01T01:01:01.722 ...

  3. js jq插件 显示中文时间戳 刚刚 N分钟前 N小时前 今天 上午 下午 日期格式化

    注:页面需提前引用JQ ; $.fn.extend({ /* ** notes: 获取13位时间戳的简单操作 ** new Date('2018-02-01 15:10:00').getTime() ...

  4. java createSQLQuery().list()返回日期格式没有时分秒的解决方法

    方法一 将Oracel数据库对应表中“收单时间的字段”receive_sheet_time,由原来的Date类型改为timestamp 然后,在java程序中,由 (java.util.timesta ...

  5. Json 返回日期格式转换

    //日期转换 function ChangeDateFormat(time) { if (time != null) { var date = new Date(parseInt(time.repla ...

  6. springmvc 格式化返回日期格式

    <mvc:annotation-driven conversion-service="conversionService"> <mvc:message-conve ...

  7. 用javascript写一个显示时间差 几分钟前 几小时前 几天前 几周前 大于一个月显示日期

    window.onload = function(){ var show_times = $(".times span"); for(var i=0;i<show_times ...

  8. POI对Excel自定义日期格式的读取

    用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel private List<String[]> rosolveFile(InputStream is, String ...

  9. poi处理excel自定义日期格式

    poi读取excel自定义时间类型时,读取到的是CELL_TYPE_NUMERIC,即数值类型,这个时候如果直接取值的话会发现取到的值和表格中的值不一样,这时应该先判断值是否是时间或者日期类型再进行处 ...

随机推荐

  1. 10 loader - 配置处理less文件的loader

    第一步:装包 cnpm i less-loader -D 安装完提示警告 peerDependencies WARNING less-loader@* requires a peer of less@ ...

  2. vs2017 c# 控制台 输出中文显示问号 ; vs2017 c# 控制台 输出中文显示乱码

    问题: 解决: 在main方法最前面加一句就OK了! Console.OutputEncoding = Encoding.GetEncoding("gbk"); 或者 Consol ...

  3. 中检测到有潜在危险的 Request.Form 值。”

    添加富文本时  如果出现" 中检测到有潜在危险的 Request.Form 值.”   却不知道怎么排错时,就在HTML   或Web表格头部添加   ValidateRequest=&qu ...

  4. learning scala Option[T]

    Option(选项)类型用来表示一个值是可选的(有值或无值). Option[T] 是一个类型为 T 的可选值的容器: 如果值存在, Option[T] 就是一个 Some[T] ,如果不存在, Op ...

  5. Python多线程笔记(二)

    Lock对象 原语锁(互斥锁)是一个同步原语,状态是"已锁定"或者"未锁定"之一.两个方法acquire()和release()用于修改锁的状态.如果状态为已锁 ...

  6. P2597 [ZJOI2012]灾难——拓扑,倍增,LCA

    最近想学支配树,但是基础还是要打好了的: P2597 [ZJOI2012]灾难 这道题是根据食物链链接出一个有向图的关系,求一个物种的灭绝会连带几种物种的灭绝: 求得就是一个点能支配几个点: 如果一个 ...

  7. chtMultiRegionSimpleFoam求解器的热源不在边界上【翻译】

    翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-solving/126777-chtmultiregionsimplefoa ...

  8. ICEM rpl文件简要讲解【转载】

    转载自:http://blog.sina.com.cn/s/blog_90affd9801016xti.html 很多人问ICEM的rpl怎样录制的问题,为什么CFX调用时老是报错,这里开个帖子简单讲 ...

  9. elasticsearch _settings

    查看所有索引的配置信息GET /_settings 查看某个索引的配置信息 GET /index_name/_settings { "my_index": { "sett ...

  10. Java网站视频资源加密

    ----------------------------------------------------------分享此文章,只为让版权能够得到更多的保护---------------------- ...