首页 > 代码库 > iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)
iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)
iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)
获取时间戳
- - (NSString *)distanceTimeWithBeforeTime:(double)beTime
- {
- NSTimeInterval now = [[NSDatedate]timeIntervalSince1970];
- double distanceTime = now - beTime;
- NSString * distanceStr;
- NSDate * beDate = [NSDatedateWithTimeIntervalSince1970:beTime];
- NSDateFormatter * df = [[NSDateFormatteralloc]init];
- [df setDateFormat:@"HH:mm"];
- NSString * timeStr = [df stringFromDate:beDate];
- [df setDateFormat:@"dd"];
- NSString * nowDay = [df stringFromDate:[NSDate date]];
- NSString * lastDay = [df stringFromDate:beDate];
- if (distanceTime < 60) {//小于一分钟
- distanceStr = @"刚刚";
- }
- else if (distanceTime <60*60) {//时间小于一个小时
- distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
- }
- else if(distanceTime <24*60*60 && [nowDayintegerValue] == [lastDay integerValue]){//时间小于一天
- distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
- }
- else if(distanceTime<24*60*60*2 && [nowDayintegerValue] != [lastDay integerValue]){
- if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDayintegerValue] > 10 && [nowDayintegerValue] == 1)) {
- distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
- }
- else{
- [df setDateFormat:@"MM-dd HH:mm"];
- distanceStr = [df stringFromDate:beDate];
- }
- }
- else if(distanceTime <24*60*60*365){
- [df setDateFormat:@"MM-dd HH:mm"];
- distanceStr = [df stringFromDate:beDate];
- }
- else{
- [df setDateFormat:@"yyyy-MM-dd HH:mm"];
- distanceStr = [df stringFromDate:beDate];
- }
- return distanceStr;
- }
iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。