iOS給個日期 判斷今天昨天前天
阿新 • • 發佈:2019-02-10
-(NSString *)compareDate:(NSDate *)date{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDatealloc] init];
NSDate *tomorrow, *yesterday;
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
// 10 first characters of description is the calendar date:
NSString * todayString = [[today description] substringToIndex:10];
NSString * yesterdayString = [[yesterday description] substringToIndex:10];
NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
NSString * dateString = [[date description] substringToIndex:10];
if ([dateString isEqualToString :todayString])
{
return@"今天";
} elseif ([dateString isEqualToString:yesterdayString])
{
return@"昨天";
}elseif ([dateString isEqualToString:tomorrowString])
{
return@"明天";
}
else
{
return dateString;
}
}
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDatealloc] init];
NSDate *tomorrow, *yesterday;
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
// 10 first characters of description is the calendar date:
NSString * todayString = [[today description] substringToIndex:10];
NSString * yesterdayString = [[yesterday description] substringToIndex:10];
NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
NSString * dateString = [[date description] substringToIndex:10];
if ([dateString isEqualToString
{
return@"今天";
} elseif ([dateString isEqualToString:yesterdayString])
{
return@"昨天";
}elseif ([dateString isEqualToString:tomorrowString])
{
return@"明天";
}
else
{
return dateString;
}
}