now in many projects, does not show the time directly, many times is to show "just", "xx minutes ago", and so on, then they are how to achieve it? 1. Create a new category for NSDate: nsdate+xmgextensionnsdate+XMGExtension.h File#import<Foundation/Foundation.h>@interfacensdate (xmgextension)-(Nsdatecomponents *) Deltafrom: (NSDate *) from;/** Whether it is this year*/-(BOOL) isthisyear;/** Whether it is today*/-(BOOL) isToday;/** Whether it was yesterday*/-(BOOL) isyesterday;@endNSDate+xmgextension.m#import "Nsdate+xmgextension.h"@implementationnsdate (xmgextension)-(Nsdatecomponents *) Deltafrom: (NSDate *) from{//CalendarNscalendar *calendar =[Nscalendar Currentcalendar]; //Compare TimeNscalendarunit unit = Nscalendarunitday | Nscalendarunitmonth | Nscalendarunityear | Nscalendarunithour | Nscalendarunitminute |Nscalendarunitsecond; return[Calendar Components:unit fromdate:from todate:self options:0];}-(BOOL) isthisyear{//CalendarNscalendar *calendar =[Nscalendar Currentcalendar]; Nsinteger Nowyear=[Calendar component:nscalendarunityear fromdate:[nsdate Date]]; Nsinteger Selfyear=[Calendar Component:nscalendarunityear fromdate:self]; returnNowyear = =selfyear;}//-(BOOL) istoday{// //Calendar//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]; returnCmps.year = =0&& Cmps.month = =0&& Cmps.day = =1; }@endusing the Create_time getter method in model XMGTopic.h file/** Posting Time*/@property (nonatomic,copy) NSString*Create_time; XMGTOPIC.M file#import "XMGTopic.h"@implementationXmgtopic-(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) {//this year if(Create.istoday) {//TodayNsdatecomponents *cmps =[[NSDate Date] deltafrom:create]; if(cmps.hour>=1) {//time gap >=1 hours return[NSString stringWithFormat:@"%zd hours ago", Cmps.hour]; }Else if(cmps.minute>=1){//time Gap >=1 minutes return[NSString stringWithFormat:@"%zd minutes ago", Cmps.minute]; }Else{//time Gap <1 minutes return @"just"; } }Else if(Create.isyesterday) {//Yesterdayfmt.dateformat=@"Yesterday hh:mm"; return[FMT stringfromdate:create]; }Else{Fmt.dateformat=@"MM-DD hh:mm"; return[FMT stringfromdate:create]; } }Else{//not this year . return_create_time; } }@endIn the xmgtopiccell.m file in view-(void) Settopic: (Xmgtopic *) topic{_topic=topic; Topic.sina_v= Arc4random_uniform (Ten)%2; //Sina Plus vSelf.sinaVView.hidden =!Topic.issina_v; //Set Avatar[Self.profileimageview Sd_setimagewithurl:[nsurl URLWithString:topic.profile_image] Placeholderimage:[uiimage Imagenamed:@"Defaultusericon"]];//Set NicknameSelf.nameLabel.text =Topic.name; //set the creation time of a postSelf.createTimeLabel.text =Topic.create_time; //Set Button text[Self SetupButtonTitle:self.dingButtton count:topic.ding placeholder:@"Top"]; [Self SetupButtonTitle:self.caiButton count:topic.cai placeholder:@"Tread"]; [Self SetupButtonTitle:self.shareButton count:topic.repost placeholder:@"Share"]; [Self SetupButtonTitle:self.commentButton count:topic.comment placeholder:@"Reviews"]; }
Specific effects
iOS Time personalization settings