IOS time format output

IOS time format output
time format output

I. Introduction

Recently, the project development process used a lot of time on the processing, the background to return to the time string to the specified format time and then displayed on the UI

For example: the back of the time to return the string 2017-04-16 13:08:06: 2017 04, 2017, 04 months, 04 months, 16, 2017-04-16, 2017-04, 04-16, 13:08, weeks and so on

The project is more developed, there is no uniform due to the early treatment time conversion problem, later found a lot of time on the conversion of the code in the project, mostly through the (-: character string array and interception) corresponding time spliced into the specified format, output in the UI display, the code is very cumbersome, and in this way is not desirable.

Reason: back to the time of the string is not 2017-04-16 13:08:06 this format, as well as 2017-04-16 this format, the need to intercept the length of the format and other checks, a lot of check code

Since it is time, we have to think through the time to complete the conversion problem, not through the interception of the string

So I wrote a class to deal with the conversion problem

Code address: see tail

Two. Effect

IOS time format output
Demo.png

Specific how to operate:

Three. Time string -> NSDate

First of all we want to 2017-04-16 13:08:06 or 2017-04-16 format time string to NSDate

We build a new NSDate Category, the author named NSDate+XHCategory, write a time string -&gt, 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;}

In order to use a string of other formats, we write down the possibilities, as follows

+ (NSDate *) xh_dateWithFormat_yyyy_MM_dd_HH_mm_string: (* NSString) string dateFormat alloc] init] {NSDateFormatter* = [[NSDateFormatter; [dateFormat setDateFormat:@ yyyy-MM-dd HH:mm NSDate *date =[dateFormat; dateFromString:string]; return date;} + (NSDate *) xh_dateWithFormat_yyyy_MM_dd_HH_string: (* NSString) string dateFormat alloc] init] {NSDateFormatter* = [[NSDateFormatter; [dateFormat setDateFormat:@ "yyyy-MM-dd HH NSDate"]; *date =[dateFormat dateFromString: string]; return date;} + (NSDate *) xh_dateWithFormat_yyyy_MM_dd_string: (* NSString) string dateFormat alloc] init] {NSDateFormatter* = [[NSDateFormatter; [dateFormat setDateFormat:@ yyyy-MM-dd NSDate *date =[dateFormat dateFromStrin]; G:string]; return date;} + (NSDate *) xh_dateWithFormat_yyyy_MM_string: (* NSString) string dateFormat alloc] init] {NSDateFormatter* = [[NSDateFormatter; [dateFormat setDateFormat:@ yyyy-MM NSDate *date =[dateFormat dateFromString:; string]; return date;}

Write a unified conversion time string for NSDate, 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 [self; date = xh_dateWithFormat_yyyy_MM_string:dateString]; if (date) return date; return nil;}

Four. Will NSDate -> NSDateFormatter

Why do you want to convert to NSDateFormatter, some people may have realized that we click on the NSDateFormatter can see 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 @property NSInteger, 5_0); weekday @property; NSInteger weekdayOrdinal; NSInteger quarter NS_AVAILABLE @ property (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 NS_AVAILABLE (10_7, 4_0); @end (*date)

We build a new NSDateComponents Category, the author named NSDateComponents+XHCategory, and the following methods:

+ (NSDateComponents *) xh_dateComponentsFromDate: (* NSDate) date [[NSCalendar currentCalendar] components:NSCalendarUnitYear| {NSDateComponents *components = NSCalendarUnitMonth NSCalendarUnitDay NSCalendarUnitWeekOfYear NSCalendarUnitHour | | | | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal fromDate:date]; return components;}

Then we can carry out the conversion operation, we create a new NSString Category, the author named NSString+XHDateFormat

In the NSString+XHDateFormat.h file, write the type you want to convert as follows:

X x / * * * * @property x April (nonatomic, copy, readonly) NSString *xh_formatNianYueRi; / * * * x x * @property (nonatomic, copy, readonly) NSString *xh_formatNianYue; / * * * x x * / @property (nonatomic, copy, readonly) NSString * xh_formatYueRi; / * * * x * (@property nonatomic, copy, readonly) NSString *xh_formatNian x x; / * * * x @property * / sec (nonatomic, copy, readonly) NSString *xh_formatShiFenMiao x x; / * * * * @property (nonatomic, copy, readonly) NSString *xh_formatShiFen; / * * * x x @property * / sec (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_f Ormat_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 - into the week @property (nonatomic, copy, readonly) NSString *xh_formatWeekDay;

The NSString+XHDateFormat.m implementation is as follows:

- (NSString * xh_formatNianYueRi) {NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@%ld%02ld%02ld April, date.year, date.month, date.day];} - (NSString * xh_formatNianYue) {NSDate *date = [NSDate xh_dateWithDateString:self] return [NSString stringWithFormat:@; "%ld%02ld", date.year, date.month];} - (NSString * xh_formatYueRi) {NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@% 02ld%02ld months, date.month, date.day];} - (NSString * xh_formatNian) {NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@ "%ld date.year]";} - (NSString * xh_formatShiFenMiao) {NSDate *date = [NSDate xh_dateWithDate String:self]; return [NSString stringWithFormat:@%ld%02ld%02ld, date.hour, date.minute, second date.seconds];} - (NSString * xh_formatShiFen) {NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@ LD%02ld%, date.hour, date.minute];} - (NSString * xh_formatFenMiao) {NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@ "%02ld%02ld, date.minute date.seconds], the second";} - (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 [NSStri Ng stringWithFormat:@ "%ld-%02ld", date.year, date.month];} - (NSString * xh_format_MM_dd) {NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@, date.month, date.day] "%02ld-%02ld";} - (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:@, date.hour, date.minute] "%02ld:%02ld";} - (NSString * xh_format_mm_ss) {NSDate *date = [NSDate xh_dateWithDateString:self]; return [NSString stringWithFormat:@, date.minute, date.seconds] "%02ld:%02ld";} - (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;}

Five. Call:

Self.timeString = @ "2017-04-16 13:08:06"; NSString *time0 = self.timeString.xh_formatWeekDay / week; //2017 04 11 16 NSString *time1 = self.timeString.xh_formatNianYueRi; *time2 = NSString //2017 04 self.timeString.xh_formatNianYue 16 NSString 04; / / *time3 = self.timeString.xh_formatYueRi; //2017 NSString *time4 = self.timeString.xh_formatNian; //13 08 minutes and 01 seconds NSString *time5 = self.timeString.xh_formatShiFenMiao; //13 08 NSString *time6 = self.timeString.xh_formatShiFen; NSString = *time7 01 seconds //08 self.timeString.xh_formatFenMiao; //2017-04-16 NSString *time8 = self.timeString.xh_format_yyyy_MM_dd; //2017-04 NSString *ti Me9 = 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 = self.timeString.xh_format_mm_ss NSString *time14;

Code address: https://github.com/CoderZhuXH/XHDate