首页 > 代码库 > 新浪微博客户端(28)-格式化日期显示
新浪微博客户端(28)-格式化日期显示
DJStatus.m
#import "DJStatus.h" #import "MJExtension.h" #import "DJPhoto.h" @implementation DJStatus + (NSDictionary *)mj_objectClassInArray { return @{@"pic_urls":[DJPhoto class]}; } // 格式化返回日期 - (NSString *)created_at { /* 将日期字符串转换为NSDate对象 */ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; NSDate *createDate = [fmt dateFromString:_created_at]; NSDate *nowDate = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; // 获取两个指定日期之前的差值 NSDateComponents *components = [calendar components:unit fromDate:createDate toDate:nowDate options:0]; if ([createDate isThisYear]) { // 今年 if ([createDate isYesterday]) { // 昨天 fmt.dateFormat = @"昨天 HH:mm"; return [fmt stringFromDate:createDate]; } else if ([createDate isToday]) { // 今天 if (components.hour >= 1) { // 大于1小时 return [NSString stringWithFormat:@"%zd小时前",components.hour]; } else if (components.minute >= 1) { // 1小时内 return [NSString stringWithFormat:@"%zd分钟前",components.minute]; } else { // 1分钟内 return @"刚刚"; } } else { // 今年的其它天 fmt.dateFormat = @"MM-dd HH:mm"; return [fmt stringFromDate:createDate]; } } else { // 非今年 fmt.dateFormat = @"yyyy-MM-dd HH:mm"; return [fmt stringFromDate:createDate]; } return _created_at; } @end
NSDate+Extension.m
#import "NSDate+Extension.h" @implementation NSDate (Extension) /** 判断当前日期是否是今年 */ - (BOOL)isThisYear { NSCalendar *calendar = [NSCalendar currentCalendar]; NSCalendarUnit yearUnit = NSCalendarUnitYear; NSDate *nowDate = [NSDate date]; NSDateComponents *createDateComponents = [calendar components:yearUnit fromDate:self]; NSDateComponents *nowDateComponents = [calendar components:yearUnit fromDate:nowDate]; return createDateComponents.year == nowDateComponents.year; } /** 判断当前日期是否是昨天 */ - (BOOL)isYesterday { NSDate *nowDate = [NSDate date]; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; NSString *createDateStr = [fmt stringFromDate:self]; NSString *nowDateStr = [fmt stringFromDate:nowDate]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *fmtCreateDate = [fmt dateFromString:createDateStr]; NSDate *fmtNowDate = [fmt dateFromString:nowDateStr]; NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents *components = [calendar components:unit fromDate:fmtCreateDate toDate:fmtNowDate options:0]; // 要求年份差值为0,月份差值为0,天数差值为1天 return (components.year == 0) && (components.month == 0) && (components.day == 1) ; } /** 判断当前日期是否是同一天 */ - (BOOL)isToday { NSDate *nowDate = [NSDate date]; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; NSString *createDateStr = [fmt stringFromDate:self]; NSString *nowDateStr = [fmt stringFromDate:nowDate]; return [createDateStr isEqualToString:nowDateStr]; } @end
最终效果:
新浪微博客户端(28)-格式化日期显示
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。