IOS time personalized settings and ios personalized settings
Currently, in many projects, the time is not displayed directly. In many cases, the words "just", "XX minutes ago" are displayed, and so on. How do they achieve this? 1. create an NSDate category: NSDate + XMGExtensionNSDate + XMGExtension. h file # import <Foundation/Foundation. h> @ interface NSDate (XMGExtension)-(NSDateComponents *) deltaFrom :( NSDate *) from;/** whether this year */-(BOOL) isThisYear; /** whether it is today */-(BOOL) isToday;/** whether it is yesterday */-(BOOL) isYesterday; @ endNSDate + XMGExtension. m # import "NSDate + XMGExtension. h "@ implementation NSDate (XMGExtension)-(NSDateComponents *) deltaFrom :( NSDate *) from {// Calendar NSCalendar * calendar = [NSCalendar currentCalendar]; // compare time NSCalendarUnit unit = calendar | NSCalendarUnitMonth | NSCalendarUnitYear | calendar; return [calendar components: unit fromDate: from toDate: self options: 0];}-(BOOL) isThisYear {// calendar NSCalendar * calendar = [NSCalendar currentCalendar]; NSInteger nowYear = [calendar compon Ent: NSCalendarUnitYear fromDate: [NSDate date]; NSInteger selfYear = [calendar component: NSCalendarUnitYear fromDate: self]; return nowYear = selfYear;} // (BOOL) isToday {// calendar // NSCalendar * calendar = [NSCalendar currentCalendar]; // NSCalendarUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; /// NSDateComponents * nowCmps = [calendar components: unit fromDate: [NSDate da Te]; // 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 stringFrom Date: self]; return [nowString is1_tostring: 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 = NSCalendarUnitYea R | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents * cmps = [calendar components: unit fromDate: selfDate toDate: nowDate options: 0]; return cmps. year = 0 & cmps. month = 0 & cmps. day = 1 ;}@ end use the create_time getter method XMGTopic in the model. hfile/** posting time */@ property (nonatomic, copy) NSString * create_time; XMGTopic. m file # import "XMGTopic. h "@ implementation XMGTopic-(NSString *) create_time {NSDateFormatt Er * 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) {// Today NSDateComponents * cmps = [[NSDate date] deltaFrom: create]; if (cmps. hour> = 1) {// time gap> = 1 hour return [NSString stringWithFormat: @ "% zd hour ago", cmps. hour];} else if (cmps. minute> = 1) {// time gap> = 1 minute return [NSString strin GWithFormat: @ "% zd Minutes Ago", cmps. minute];} else {// time gap <1 minute return @ "just" ;}} else if (create. isYesterday) {// Yesterday fmt. dateFormat = @ "Yesterday HH: mm"; return [fmt stringFromDate: create];} else {fmt. dateFormat = @ "MM-dd HH: mm"; return [fmt stringFromDate: create] ;}} else {// not this year's return _ create_time ;}} @ end XMGTopicCell in the view. in the m file-(void) setTopic :( XMGTopic *) topic {_ topic = topic; topic. sina_v = arc4random_uniform (10) % 2; // SINA + V self. sinaVView. hidden =! Topic. isSina_v; // set the Avatar [self. profileImageView sd_setImageWithURL: [NSURL URLWithString: topic. profile_image] placeholderImage: [UIImage imageNamed: @ "defaultUserIcon"]; // sets the nickname self. nameLabel. text = topic. name; // set the post creation time self. createTimeLabel. text = topic. create_time; // set the button text [self setupButtonTitle: self. dingButtton count: topic. ding placeholder: @ "TOP"]; [self setupButtonTitle: self. caiButton count: topic. cai placeholder: @ "Step"]; [self setupButtonTitle: self. shareButton count: topic. repost placeholder: @ "share"]; [self setupButtonTitle: self. commentButton count: topic. comment placeholder: @ "comment"];}
Effect