
I. Preface
Recently, a lot of time processing has been used in the process of project development. The time string returned from the background is converted to the specified format time and then displayed on the UI.
For example, the time string 2017-04-16 13:08:06 returned from the background is converted to: April 16, 2017, April 16, 2017-04-16, 04-16, 13:08, what day of the week, etc.
The project is multi-person development, because there is no unified processing of time conversion in the early stage, later found that many of the code about time conversion in the project, most of them are through (-: equal characters) intercepted into a string array, then take the corresponding time to splice into a specified format, output in the UI display place, the code is very bulky, and this way. Very undesirable.
Reason: Not all the time strings returned from the background are in 2017-04-16 13:08:06 format, but also in 2017-04-16 format. Before interception, the length format and other checking codes are needed, and many more checking codes are needed. It is very undesirable.
Since it's time, we need to think through time to complete the conversion problem, not by intercepting strings.
So I wrote a class to deal with the transformation problem in a unified way.
Code Address: See Film End
II. Effectiveness

Specific how to operate:
3. Put the time string - > NSDate
First, we need to convert the format time string 2017-04-16 13:08:06 or 2017-04-16 to NSDate.
We create a new Category of NSDate. The author named it NSDate+XHCategory and wrote a time string - > NSDate method. The code is as follows:
+(NSDate*)xh_dateWithFormat_yyyy_MM_dd_HH_mm_ss_string:(NSString *)string { NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date =[dateFormat dateFromString:string]; return date; }
To incorporate other format time strings, let's write down the possible scenarios as follows
+(NSDate *)xh_dateWithFormat_yyyy_MM_dd_HH_mm_string:(NSString *)string { NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate *date =[dateFormat dateFromString:string]; return date; } +(NSDate *)xh_dateWithFormat_yyyy_MM_dd_HH_string:(NSString *)string { NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH"]; NSDate *date =[dateFormat dateFromString:string]; return date; } +(NSDate *)xh_dateWithFormat_yyyy_MM_dd_string:(NSString *)string { NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; NSDate *date =[dateFormat dateFromString:string]; return date; } +(NSDate *)xh_dateWithFormat_yyyy_MM_string:(NSString *)string { NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM"]; NSDate *date =[dateFormat dateFromString:string]; return date; }
Write a unified conversion time string to NSDate method, as follows:
+(NSDate *)xh_dateWithDateString:(NSString *)dateString { NSDate *date = nil; date = [self xh_dateWithFormat_yyyy_MM_dd_HH_mm_ss_string:dateString]; if(date) return date; date = [self xh_dateWithFormat_yyyy_MM_dd_HH_mm_string:dateString]; if(date) return date; date = [self xh_dateWithFormat_yyyy_MM_dd_HH_string:dateString]; if(date) return date; date = [self xh_dateWithFormat_yyyy_MM_dd_string:dateString]; if(date) return date; date = [self xh_dateWithFormat_yyyy_MM_string:dateString]; if(date) return date; return nil; }
IV. NSDate - > NSDate Formatter
Why do you want to convert to NSDateFormatter again? Some people may already understand that when we click on NSDateFormatter, we can see that NSDateFormatter has the following attributes
@property (nullable, copy) NSCalendar *calendar NS_AVAILABLE(10_7, 4_0); @property (nullable, copy) NSTimeZone *timeZone NS_AVAILABLE(10_7, 4_0); @property NSInteger era; @property NSInteger year; @property NSInteger month; @property NSInteger day; @property NSInteger hour; @property NSInteger minute; @property NSInteger second; @property NSInteger nanosecond NS_AVAILABLE(10_7, 5_0); @property NSInteger weekday; @property NSInteger weekdayOrdinal; @property NSInteger quarter NS_AVAILABLE(10_6, 4_0); @property NSInteger weekOfMonth NS_AVAILABLE(10_7, 5_0); @property NSInteger weekOfYear NS_AVAILABLE(10_7, 5_0); @property NSInteger yearForWeekOfYear NS_AVAILABLE(10_7, 5_0); @property (getter=isLeapMonth) BOOL leapMonth NS_AVAILABLE(10_8, 6_0); @property (nullable, readonly, copy) NSDate *date NS_AVAILABLE(10_7, 4_0); @end
We build a new Category of NSDate Components. The author named NSDate Components + XHCategory and implemented the following methods:
+(NSDateComponents *)xh_dateComponentsFromDate:(NSDate *)date { NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal fromDate:date]; return components; }
Then we can do the conversion. We create a new Category of NSString, named NSString+XHDateFormat.
Write in the NSString+XHDateFormat.h file the types to be converted as follows:
/** * x Year x, month x */ @property(nonatomic,copy,readonly)NSString *xh_formatNianYueRi; /** * x Year x Month */ @property(nonatomic,copy,readonly)NSString *xh_formatNianYue; /** * x Month x Day */ @property(nonatomic,copy,readonly)NSString *xh_formatYueRi; /** * x year */ @property(nonatomic,copy,readonly)NSString *xh_formatNian; /** * x X minutes x seconds */ @property(nonatomic,copy,readonly)NSString *xh_formatShiFenMiao; /** * x Time x minute */ @property(nonatomic,copy,readonly)NSString *xh_formatShiFen; /** * x Minutes x seconds */ @property(nonatomic,copy,readonly)NSString *xh_formatFenMiao; /** * yyyy-MM-dd */ @property(nonatomic,copy,readonly)NSString *xh_format_yyyy_MM_dd; /** * yyyy-MM */ @property(nonatomic,copy,readonly)NSString *xh_format_yyyy_MM; /** * MM-dd */ @property(nonatomic,copy,readonly)NSString *xh_format_MM_dd; /** * yyyy */ @property(nonatomic,copy,readonly)NSString *xh_format_yyyy; /** * HH:mm:ss */ @property(nonatomic,copy,readonly)NSString *xh_format_HH_mm_ss; /** * HH:mm */ @property(nonatomic,copy,readonly)NSString *xh_format_HH_mm; /** * mm:ss */ @property(nonatomic,copy,readonly)NSString *xh_format_mm_ss; #pragma mark - Converts to a day of the week @property(nonatomic,copy,readonly)NSString *xh_formatWeekDay;
The implementation in NSString+XHDateFormat.m is as follows:
-(NSString *)xh_formatNianYueRi { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld year%02ld month%02ld day",date.year,date.month,date.day]; } -(NSString *)xh_formatNianYue { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld year%02ld month",date.year,date.month]; } -(NSString *)xh_formatYueRi { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%02ld month%02ld month",date.month,date.day]; } -(NSString *)xh_formatNian { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld year",date.year]; } -(NSString *)xh_formatShiFenMiao { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld time%02ld branch%02ld second",date.hour,date.minute,date.seconds]; } -(NSString *)xh_formatShiFen { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld time%02ld branch",date.hour,date.minute]; } -(NSString *)xh_formatFenMiao { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%02ld branch%02ld second",date.minute,date.seconds]; } -(NSString *)xh_format_yyyy_MM_dd { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld-%02ld-%02ld",date.year,date.month,date.day]; } -(NSString *)xh_format_yyyy_MM { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld-%02ld",date.year,date.month]; } -(NSString *)xh_format_MM_dd { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%02ld-%02ld",date.month,date.day]; } -(NSString *)xh_format_yyyy { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%ld",date.year]; } -(NSString *)xh_format_HH_mm_ss { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",date.hour,date.minute,date.seconds]; } -(NSString *)xh_format_HH_mm { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%02ld:%02ld",date.hour,date.minute]; } -(NSString *)xh_format_mm_ss { NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@"%02ld:%02ld",date.minute,date.seconds]; } -(NSString *)xh_formatWeekDay { NSString *weekStr=nil; NSDate *date = [NSDate xh_dateWithDateString:self]; switch (date.weekday) { case 2: weekStr = @"Monday"; break; case 3: weekStr = @"Tuesday"; break; case 4: weekStr = @"Wednesday"; break; case 5: weekStr = @"Thursday"; break; case 6: weekStr = @"Friday"; break; case 7: weekStr = @"Saturday"; break; case 1: weekStr = @"Sunday"; break; default: break; } return weekStr; }
5. Call:
self.timeString = @"2017-04-16 13:08:06"; //week NSString *time0 = self.timeString.xh_formatWeekDay; //16 April 2017 NSString *time1 = self.timeString.xh_formatNianYueRi; //April 2017 NSString *time2 = self.timeString.xh_formatNianYue; //16 April NSString *time3 = self.timeString.xh_formatYueRi; //2017 NSString *time4 = self.timeString.xh_formatNian; //13:08:01 seconds NSString *time5 = self.timeString.xh_formatShiFenMiao; //13:08 NSString *time6 = self.timeString.xh_formatShiFen; //08 minutes 01 seconds NSString *time7 = self.timeString.xh_formatFenMiao; //2017-04-16 NSString *time8 = self.timeString.xh_format_yyyy_MM_dd; //2017-04 NSString *time9 = self.timeString.xh_format_yyyy_MM; //04-16 NSString *time10 = self.timeString.xh_format_MM_dd; //2017 NSString *time11 = self.timeString.xh_format_yyyy; //13:08:06 NSString *time12 = self.timeString.xh_format_HH_mm_ss; //13:08 NSString *time13 = self.timeString.xh_format_HH_mm; //08:06 NSString *time14 = self.timeString.xh_format_mm_ss;
Code address: https://github.com/CoderZhuXH/XHDate