Simple IOS date format display, ios display
First of all, I want to explain the meaning of the title and the concise format of the date. The reason for the introduction is that it makes people clear at a glance and does not need to think about the time.
In details, we see the time format in the circle of friends.
For example, just-a few minutes ago-a few hours ago.
What we bring here today is a simple category.
Convenient and practical.
And simple practical applications.
Let's take a look at what the category looks like.
. H
#import <Foundation/Foundation.h>@interface NSDate (CXExtension)-(NSDateComponents *)dateFrom:(NSDate *)from;-(BOOL)isThisYear;-(BOOL)isToday;-(BOOL)isYesterday;@end
. M
# Import "NSDate + CXExtension. h "@ implementation NSDate (CXExtension)-(NSDateComponents *) dateFrom :( NSDate *) from {// calendar NSCalendar * calendar = [NSCalendar currentCalendar]; NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | region | NSCalendarUnitMinute | NSCalendarUnitSecond; return [calendar components: unit fromDate: from toDate: self options: 0];} // determine whether it is this year-(BOOL) isThisYear {NSCalendar * calendar = [NSCalendar currentCalendar]; NSInteger nowYear = [calendar component: NSCalendarUnitYear fromDate: [NSDate date]; NSInteger selfYear = [calendar component: NSCalendarUnitYear fromDate: self]; return selfYear = nowYear;} // determines whether it is today-(BOOL) isToday {NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "yyyy-MM-dd"; NSString * nowDate = [formatter stringFromDate: [NSDate date]; NSString * selfDate = [formatter stringFromDate: self]; return [nowDate is1_tostring: selfDate];} // determines whether it was yesterday-(BOOL) isYesterday {NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "yyyy-MM-dd"; NSDate * nowDate = [formatter dateFromString: [formatter stringFromDate: [NSDate date]; NSDate * selfdate = [formatter dateFromString: [formatter stringFromDate: self]; NSCalendar * calendar = [NSCalendar currentCalendar]; NSDateComponents * unit = [calendar components: Required | NSCalendarUnitMonth | required fromDate: selfdate toDate: nowDate options: 0]; return unit. year = 0 & unit. month = 0 & unit. day = 1 ;}@ end
The following strength shows the effect:
When the time is last year, all time formats are displayed
When the time is this year
When the time is today, it is used to determine the specific time
Generated several hours ago
A few minutes ago
Display format
When the time is yesterday, it indicates yesterday and the hour, minute, and second.
When the time is neither today nor yesterday
Display All time formats except year
-(NSString *) create_time {NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "yyyy-MM-dd HH: mm: ss"; NSDate * creat = [formatter dateFromString: _ create_time]; if (creat. isThisYear) {if (creat. isToday) {NSDateComponents * unit = [creat dateFrom: creat]; if (unit. hour> 1) {return [NSString stringWithFormat: @ "% zd hour ago", unit. hour];} else if (unit. minute> 1) {return [NSString stringWithFormat: @ "% zd Minutes Ago", unit. minute];} else {return @ "just";} else if (creat. isYesterday) {formatter. dateFormat = @ "Yesterday HH: mm: ss"; return [formatter stringFromDate: creat];} else {formatter. dateFormat = @ "MM-dd HH: mm: ss"; return [formatter stringFromDate: creat] ;}} else {return [formatter stringFromDate: creat] ;}}