- (NSString *)compareDate:(NSDate *)date{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] 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 @" 今天";
} else if ([dateString isEqualToString:yesterdayString])
{
return @" 昨天";
}else if ([dateString isEqualToString:tomorrowString])
{
return @" 明天";
}
else
{
return dateString;
}
}
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] 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 @" 今天";
} else if ([dateString isEqualToString:yesterdayString])
{
return @" 昨天";
}else if ([dateString isEqualToString:tomorrowString])
{
return @" 明天";
}
else
{
return dateString;
}
}
//----------------------------------------------
/**
///// 和當前時間比較
//// 1)1分鍾以內 顯示 : 剛剛
//// 2)1小時以內 顯示 : X分鍾前
/// 3)今天或者昨天 顯示 : 今天 09:30 昨天 09:30
/// 4) 今年顯示 : 09月12日
/// 5) 大於本年 顯示 : 2013/09/09
**/
+ (NSString *)formateDate:(NSString *)dateString withFormate:(NSString *) formate
{
@try {
//實例化一個NSDateFormatter對象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formate];
NSDate * nowDate = [NSDate date];
///// 將需要轉換的時間轉換成 NSDate 對象
NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
///// 取當前時間和轉換時間兩個日期對象的時間間隔
///// 這里的NSTimeInterval 並不是對象,是基本型,其實是double類型,是由c定義的: typedef double NSTimeInterval;
NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
//// 再然后,把間隔的秒數折算成天數和小時數:
NSString *dateStr = @"";
if (time<=60) { //// 1分鍾以內的
dateStr = @"剛剛";
}else if(time<=60*60){ //// 一個小時以內的
int mins = time/60;
dateStr = [NSString stringWithFormat:@"%d分鍾前",mins];
}else if(time<=60*60*24){ //// 在兩天內的
[dateFormatter setDateFormat:@"YYYY/MM/dd"];
NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
[dateFormatter setDateFormat:@"HH:mm"];
if ([need_yMd isEqualToString:now_yMd]) {
//// 在同一天
dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
}else{
//// 昨天
dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
}
}else {
[dateFormatter setDateFormat:@"yyyy"];
NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
NSString *nowYear = [dateFormatter stringFromDate:nowDate];
if ([yearStr isEqualToString:nowYear]) {
//// 在同一年
[dateFormatter setDateFormat:@"MM月dd日"];
dateStr = [dateFormatter stringFromDate:needFormatDate];
}else{
[dateFormatter setDateFormat:@"yyyy/MM/dd"];
dateStr = [dateFormatter stringFromDate:needFormatDate];
}
}
return dateStr;
}
@catch (NSException *exception) {
return @"";
}
}
///// 和當前時間比較
//// 1)1分鍾以內 顯示 : 剛剛
//// 2)1小時以內 顯示 : X分鍾前
/// 3)今天或者昨天 顯示 : 今天 09:30 昨天 09:30
/// 4) 今年顯示 : 09月12日
/// 5) 大於本年 顯示 : 2013/09/09
**/
+ (NSString *)formateDate:(NSString *)dateString withFormate:(NSString *) formate
{
@try {
//實例化一個NSDateFormatter對象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formate];
NSDate * nowDate = [NSDate date];
///// 將需要轉換的時間轉換成 NSDate 對象
NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
///// 取當前時間和轉換時間兩個日期對象的時間間隔
///// 這里的NSTimeInterval 並不是對象,是基本型,其實是double類型,是由c定義的: typedef double NSTimeInterval;
NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
//// 再然后,把間隔的秒數折算成天數和小時數:
NSString *dateStr = @"";
if (time<=60) { //// 1分鍾以內的
dateStr = @"剛剛";
}else if(time<=60*60){ //// 一個小時以內的
int mins = time/60;
dateStr = [NSString stringWithFormat:@"%d分鍾前",mins];
}else if(time<=60*60*24){ //// 在兩天內的
[dateFormatter setDateFormat:@"YYYY/MM/dd"];
NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
[dateFormatter setDateFormat:@"HH:mm"];
if ([need_yMd isEqualToString:now_yMd]) {
//// 在同一天
dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
}else{
//// 昨天
dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
}
}else {
[dateFormatter setDateFormat:@"yyyy"];
NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
NSString *nowYear = [dateFormatter stringFromDate:nowDate];
if ([yearStr isEqualToString:nowYear]) {
//// 在同一年
[dateFormatter setDateFormat:@"MM月dd日"];
dateStr = [dateFormatter stringFromDate:needFormatDate];
}else{
[dateFormatter setDateFormat:@"yyyy/MM/dd"];
dateStr = [dateFormatter stringFromDate:needFormatDate];
}
}
return dateStr;
}
@catch (NSException *exception) {
return @"";
}
}