后台返回的格式如下:
实现输出如下:
我的处理如下:
// 处理数据 2017-11-28T02:41:09.487Z // 请求的时间戳。日期格式按照ISO8601标准表示,并需要使用UTC时间。 // 去掉.之后的字符串 NSArray *strArray = [time componentsSeparatedByString:@"."]; // 字符串转date NSDate *registerDate = [NSString dateFromString:[NSString stringWithFormat:@"%@Z",strArray[0]]]; // 对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 = 0; NSString *result; if (timeInterval/60 < 1){ result = [NSString stringWithFormat:@"刚刚"]; } else if((temp = timeInterval/60) <60){ result = [NSString stringWithFormat:@"%ld分钟前",temp]; } else if((temp = temp/60) <24){ result = [NSString stringWithFormat:@"%ld小时前",temp]; } else if((temp = temp/24) <30){ result = [NSString stringWithFormat:@"%ld天前",temp]; } else if((temp = temp/30) <12){ result = [NSString stringWithFormat:@"%ld月前",temp]; } else{ temp = temp/12; 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]/1000; // 时间差 NSTimeInterval time = currentTime - createTime; //秒转分钟 NSInteger small = time / 60; if (small == 0) { return [NSString stringWithFormat:@"刚刚"]; } if (small < 60) { return [NSString stringWithFormat:@"%ld分钟前",small]; } // 秒转小时 NSInteger hours = time/3600; if (hours<24) { return [NSString stringWithFormat:@"%ld小时前",hours]; } //秒转天数 NSInteger days = time/3600/24; if (days < 30) { return [NSString stringWithFormat:@"%ld天前",days]; } //秒转月 NSInteger months = time/3600/24/30; if (months < 12) { return [NSString stringWithFormat:@"%ld月前",months]; } //秒转年 NSInteger years = time/3600/24/30/12; 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 = -1; 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 = 0 - [theDate timeIntervalSinceDate:[NSDate date]]; if (cha/3600 < 1) { timeString = [NSString stringWithFormat:@"%f", cha/60]; timeString = [timeString substringToIndex:timeString.length-7]; int num= [timeString intValue]; if (num <= 1) { timeString = [NSString stringWithFormat:@"刚刚"]; }else{ timeString = [NSString stringWithFormat:@"%@分钟前", timeString]; } } if (cha/3600 > 1 && cha/86400 < 1) { timeString = [NSString stringWithFormat:@"%f", cha/3600]; timeString = [timeString substringToIndex:timeString.length-7]; timeString = [NSString stringWithFormat:@"%@小时前", timeString]; } if (cha/86400 > 1){ timeString = [NSString stringWithFormat:@"%f", cha/86400]; timeString = [timeString substringToIndex:timeString.length-7]; int num = [timeString intValue]; if (num < 2) { timeString = [NSString stringWithFormat:@"昨天"]; } else{ timeString =[dateFormatter stringFromDate:theDate]; } } return timeString; }
以前没遇到过这个格式的日期,今天算是长见识了,哈哈。希望你遇到这日期能看到我的博客,哈哈?
© 著作权归作者所有
举报
发表评论
0/200