NSDate+Extension.h:
#import@interface NSDate (Extension)
/**
* 是否为今天
*/
- (BOOL)isToday;
/**
* 是否为昨天
*/
- (BOOL)isYesterday;
/**
* 是否为今年
*/
- (BOOL)isThisYear;
/**
* 返回一个只有年月日的时间
*/
- (NSDate *)dateWithYMD;
/**
* 获得与当前时间的差距
*/
- (NSDateComponents *)deltaWithNow;
@end
_______________________________________________________________________
NSDate+Extension.m :
#import "NSDate+Extension.h"
@implementation NSDate (Extension)
/**
* 是否为今天
*/
- (BOOL)isToday
{
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
// 1.获得当前时间的年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
// 2.获得self的年月日
NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
return
(selfCmps.year == nowCmps.year) &&
(selfCmps.month == nowCmps.month) &&
(selfCmps.day == nowCmps.day);
}
/**
* 是否为昨天
*/
- (BOOL)isYesterday
{
// 2014-05-01
NSDate *nowDate = [[NSDate date] dateWithYMD];
// 2014-04-30
NSDate *selfDate = [self dateWithYMD];
// 获得nowDate和selfDate的差距
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
return cmps.day == 1;
}
- (NSDate *)dateWithYMD
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *selfStr = [fmt stringFromDate:self];
return [fmt dateFromString:selfStr];
}
/**
* 是否为今年
*/
- (BOOL)isThisYear
{
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitYear;
// 1.获得当前时间的年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
// 2.获得self的年月日
NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
return nowCmps.year == selfCmps.year;
}
- (NSDateComponents *)deltaWithNow
{
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
}
@end
__________________________________________________________________________
一定要调用上面的NSDate+Extension.h文件
//设置时间显示格式
-(NSString *)created_at
{
// 字符串转换NSDate
// 日期格式字符串
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"EEE MMM d HH:mm:ss Z yyyy";
// 设置格式本地化,日期格式字符串需要知道是哪个国家的日期,才知道怎么转换
fmt.locale = [NSLocale localeWithLocaleIdentifier:@"en_us"];
NSDate *created_at = [fmt dateFromString:_created_at];
if ([created_at isThisYear]) { // 今年
if ([created_at isToday]) { // 今天
// 计算跟当前时间差距
NSDateComponents *cmp = [created_at deltaWithNow];
if (cmp.hour >= 1) {
return [NSString stringWithFormat:@"%ld小时之前",(long)cmp.hour];
}else if (cmp.minute > 1){
return [NSString stringWithFormat:@"%ld分钟之前",(long)cmp.minute];
}else{
return @"刚刚";
}
}else if ([created_at isYesterday]){ // 昨天
fmt.dateFormat = @"昨天 HH:mm";
return [fmt stringFromDate:created_at];
}else{ // 前天
fmt.dateFormat = @"MM-dd HH:mm";
return [fmt stringFromDate:created_at];
}
}else{ // 不是今年
fmt.dateFormat = @"yyyy-MM-dd HH:mm";
return [fmt stringFromDate:created_at];
}
return _created_at;
}