+ (NSString *)stringDate:(NSDate *)date{
NSString *strDate = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
NSDate *today = [NSDate date];
NSString *strToday = [NSDateFormatter localizedStringFromDate:today dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
if ([strToday isEqualToString:strDate]){
return NSLocalizedString(@"today", nil);
}
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc]init];
[comps setDay:-1];
NSDate *yesterday = [cal dateByAddingComponents:comps toDate:today options:0];
NSString *strYesterday = [NSDateFormatter localizedStringFromDate:yesterday dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
if([strYesterday isEqualToString:strDate]){
return NSLocalizedString(@"Yesterday", nil);
}
return strDate;
}
今日や昨日(場合によっては明日等)は、日付の文字列(例 2014/01/01)を返すよりも、今日や昨日と返した方がユーザーにはわかりやすい場合があるので、そんなとき用に。