1. 程式人生 > >iOS給個日期 判斷今天昨天前天

iOS給個日期 判斷今天昨天前天

-(NSString *)compareDate:(NSDate *)date{

NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDateallocinit];
NSDate *tomorrow, *yesterday;

    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];

// 10 first characters of description is the calendar date:

NSString * todayString = [[today descriptionsubstringToIndex:10];
NSString * yesterdayString = [[yesterday descriptionsubstringToIndex:10];
NSString * tomorrowString = [[tomorrow descriptionsubstringToIndex:10];

NSString * dateString = [[date descriptionsubstringToIndex:10];

if ([dateString isEqualToString
:todayString])
    {
return@"今天";
    } elseif ([dateString isEqualToString:yesterdayString])
    {
return@"昨天";
    }elseif ([dateString isEqualToString:tomorrowString])
    {
return@"明天";
    }
else
    {
return dateString;
    }
}