1. Create a nsdate category
. h method
-(NSString *) dateamity;
The. M method implements
-(NSString *) dateamity
{
BOOL Isthisyear=false,isthismonth=false,isthisday=false,isyesterday=false,istomorrow=false;
NSString *str = @ "";
NSDate *now = [NSDate Date];
Nsdatecomponents *nowcoms = [[Nscalendar Currentcalendar] components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday Fromdate:now];
Nsdatecomponents *selfcoms = [[Nscalendar Currentcalendar] components:nscalendarunityear| nscalendarunitmonth| Nscalendarunitday Fromdate:self];
if (nowcoms.year = = selfcoms.year) {
Isthisyear = true;
}
if (Nowcoms.month = = Selfcoms.month && isthisyear) {
Isthismonth = true;
}
if (Nowcoms.day = = Selfcoms.day && isthismonth) {
Isthisday = true;
}else if (nowcoms.day = = selfcoms.day+1 && isthismonth) {
Isyesterday = true;
}else if (nowcoms.day = = Selfcoms.day-1 && isthismonth) {
Istomorrow = true;
}
NSDateFormatter *dateformate = [NSDateFormatter new];
Dateformate.dateformat = @ "Yyyy/mm/dd hh:mm";
NSString *header = @ "";
if (isthisyear) {
Dateformate.dateformat = @ "Mm/dd hh:mm";
if (isthisday) {
Header = @ "Today";
Header = @ "";
}else if (isyesterday) {
Header = @ "Yesterday";
}else if (istomorrow) {
Header = @ "Tomorrow";
}
if (![ Header isequaltostring:@ ""]) {
Dateformate.dateformat = @ "hh:mm";
}
}
str = [NSString stringwithformat:@ "%@%@", Header,[dateformate Stringfromdate:self]];
return str;
}
Nsdate+category date output to today or yesterday 09:00