1. 创建一个NSDate 的类别
.h 方法
-(NSString *)dateAmity;
.m方法实现
-(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 = @"今天 ";
header = @" ";
}else if (isYesterDay) {
header = @"昨天 ";
}else if (isTomorrow) {
header = @"明天 ";
}
if (![header isEqualToString:@""]) {
dateFormate.dateFormat = @"HH:mm";
}
}
str = [NSString stringWithFormat:@"%@%@",header,[dateFormate stringFromDate:self]];
return str;
}