返回日期格式: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,即数值类型,这个时候如果直接取值的话会发现取到的值和表格中的值不一样,这时应该先判断值是否是时间或者日期类型再进行处 ...
随机推荐
- javascript常用工具类util.js
//如果大家想要补充,请留言 /** * 判断指定名称的复选框是否被选中 * * @param {} * chname复选框名称 */ function chkCheckCha(chname) { v ...
- hibernate meger
转: 在Hibernate中,有save.persist.savaOrUpdate.merge等方法有插入数据的功能.前三者理解起来较后者容易一些,merge方法从api中的介绍就看以看出它是最复杂的 ...
- 专为简化 C 开发而设计的编程语言 Trad
Trad 是一个专为简化 C 开发而设计的编程语言,它: 基于 C: 经编译器编译后能得到干净可读的标准 C 代码,其与 C 的关系就像 TypeScript 与 JavaScript 一样 专为 U ...
- ServletRequest、 HttpServletRequest、Request的联系与区别
一. servlet理论上可以处理多种形式的请求响应形式 http只是其中之一 所以HttpServletRequest HttpServletResponse分别是ServletRequest和Se ...
- 是什么是FBC CBV
- FBV url - 函数 - CBV url - view
- BZOJ1209 最佳包裹 (三维凸包 增量法)
题意 求三维凸包的表面积. N≤100N\le100N≤100 题解 暴力往当前的凸包里加点.O(n2)O(n^2)O(n2).题解详见大佬博客 扰动函数shakeshakeshake是为了避免四点共 ...
- man与info
Linux系统中在线求助命令:man page 与info page 还有--help . --help没有man的详细,首先我们来看mna 命令.在linux中输入 man + 相关的文件 ,就可以 ...
- 006_Python3 数字(Number)
1. Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象将被创建: var1 = ...
- Shadows 使用说明
1:下载最新版 Windows地址:点击下载 Mac地址:点击下载 2:Windows安装插件(点击下方插件名即可下载) .NET Framework 4.7.2和 Microsoft Visual ...
- Turn Off Windows Firewall Using PowerShell and CMD
If you want to turn off the Windows Firewall, there are three methods. One is using the GUI which is ...