具體效果 ...
現在在很多項目中,不會直接顯示時間,很多時候都是顯示“剛剛”,”XX分鐘前”,等等字樣,那麼他們是怎麼實現的呢 ? 1.新建一個NSDate的類目:NSDate+XMGExtension NSDate+XMGExtension.h 文件 #import <Foundation/Foundation.h> @interface NSDate (XMGExtension) -(NSDateComponents *)deltaFrom:(NSDate *)from; /** 是否是今年 */ -(BOOL)isThisYear; /** 是否是今天 */ -(BOOL)isToday; /** 是否是昨天 */ -(BOOL)isYesterday; @end NSDate+XMGExtension.m #import "NSDate+XMGExtension.h" @implementation NSDate (XMGExtension) -(NSDateComponents *)deltaFrom:(NSDate *)from{ // 日曆 NSCalendar *calendar = [NSCalendar currentCalendar]; // 比較時間 NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth |NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ; return [calendar components:unit fromDate:from toDate:self options:0]; } -(BOOL)isThisYear{ // 日曆 NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]]; NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self]; return nowYear == selfYear; } //-(BOOL)isToday{ // // 日曆 // NSCalendar *calendar = [NSCalendar currentCalendar]; // // NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; // // NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]]; // // NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; // // return nowCmps.year == selfCmps.year // && nowCmps.month == selfCmps.month // && nowCmps.day == selfCmps.day; // // //} -(BOOL)isToday{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSString *nowString = [fmt stringFromDate:[NSDate date]]; NSString *selfString = [fmt stringFromDate:self]; return [nowString isEqualToString:selfString]; } -(BOOL)isYesterday{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]]; NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0]; return cmps.year == 0 && cmps.month == 0 && cmps.day == 1; } @end 在model中使用 create_time getter方法 XMGTopic.h文件 /** 發帖時間 */ @property(nonatomic,copy)NSString *create_time; XMGTopic.m文件 #import "XMGTopic.h" @implementation XMGTopic -(NSString *)create_time{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *create = [fmt dateFromString:_create_time]; if (create.isThisYear) { //今年 if (create.isToday) { //今天 NSDateComponents *cmps = [[NSDate date] deltaFrom:create]; if (cmps.hour>=1) { //時間差距>=1小時 return [NSString stringWithFormat:@"%zd小時前",cmps.hour]; }else if(cmps.minute>=1){ //時間差距>=1分分鐘 return [NSString stringWithFormat:@"%zd分鐘前",cmps.minute]; }else{//時間差距<1分分鐘 return @"剛剛"; } }else if(create.isYesterday){ //昨天 fmt.dateFormat= @"昨天 HH:mm"; return [fmt stringFromDate:create]; }else{ fmt.dateFormat= @"MM-dd HH:mm"; return [fmt stringFromDate:create]; } }else{ //不是今年 return _create_time; } } @end 在view中XMGTopicCell.m文件中 -(void)setTopic:(XMGTopic *)topic{ _topic = topic; topic.sina_v = arc4random_uniform(10)%2; // 新浪加V self.sinaVView.hidden = !topic.isSina_v; // 設置頭像 [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]]; // 設置昵稱 self.nameLabel.text = topic.name; // 設置帖子的創建時間 self.createTimeLabel.text = topic.create_time; // 設置按鈕文字 [self setupButtonTitle:self.dingButtton count:topic.ding placeholder:@"頂"]; [self setupButtonTitle:self.caiButton count:topic.cai placeholder:@"踩"]; [self setupButtonTitle:self.shareButton count:topic.repost placeholder:@"分享"]; [self setupButtonTitle:self.commentButton count:topic.comment placeholder:@"評論"]; }
具體效果