iOS时间个性化设置设置

现在在很多项目中,不会直接显示时间,很多时候都是显示“刚刚”,”XX分钟前”,等等字样,那么他们是怎么实现的呢 ?

.新建一个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:];
} -(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:]; return cmps.year ==
&& cmps.month ==
&& cmps.day == ; } @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小时
return [NSString stringWithFormat:@"%zd小时前",cmps.hour];
}else if(cmps.minute>=){ //时间差距>=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()%; // 新浪加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:@"评论"]; }

具体效果

iOS时间个性化设置设置

上一篇:TCP首部解析


下一篇:面试3——java集合类总结(Set)