enum{
Sun = 1,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
};
- (NSString *)dateToStr:(NSDate *)date
{
NSString *flagString = nil;
// 当前日期
int currentDate = [self getIntegerFromDate:[NSDate date]];
// 目标日期
int tar = [self getIntegerFromDate:date];
// 时间差
int diff = tar - currentDate;
if ( diff < 3 && diff >= 0)
{
switch (diff)
{
case 0:
flagString = @"今天";
break;
case 1:
flagString = @"明天";
break;
case 2:
flagString = @"后天";
break;
default:
break;
}
}
else if (diff == -1)
{
flagString = @"昨天";
}
else
{
NSCalendar* clendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSChineseCalendar];
NSUInteger unitFlags = NSWeekdayCalendarUnit;
NSDateComponents *cps = [clendar components:unitFlags fromDate:date ];
switch ([cps weekday]) {
case Sun:
flagString = @"星期天";
break;
case Mon:
flagString = @"星期一";
break;
case Tue:
flagString = @"星期二";
break;
case Wed:
flagString = @"星期三";
break;
case Thu:
flagString = @"星期四";
break;
case Fri:
flagString = @"星期五";
break;
case Sat:
flagString = @"星期六";
break;
default:
break;
}
}
return flagString ;
}
- (int)getIntegerFromDate:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyyMMdd"];
NSString *todayStr = [formatter stringFromDate:date];
int presentDay = [todayStr intValue];
return presentDay;
}
网友评论