标签:
- (NSString *)created_at{
// 从后台返回的字符串格式:Mon Aug 03 09:17:31 +0800 2014,
//NSDateFormatter可以把NSDate转成字符串,也可以把字符串转成NSDate
//初始化一个时间格式化器
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//指定时间格式化字符串-->是一个什么样的格式
formatter.dateFormat = @"EEE MMM dd HH:mm:ss z yyyy";
// formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//如果是真机,一定要指定个这locale,标明这个时间是什么样标识
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
//当前w创建的时间
NSDate *createDate = [formatter dateFromString:_created_at];
// //取出当前时间
NSDate *currentDate = [NSDate date];
//拿着当前时间与create_at进行一个对比
// if (是今年) {
if ([self isThisYearWithDate:createDate]) {
//是同一年
//判断是否是今天
if ([self isTodayWithDate:createDate]) {
//是今天
//如果创建时间加上60秒,大于当前时间,代表1分钟之内
NSDate *resultDate = [createDate dateByAddingTimeInterval:60];
if ([resultDate compare:currentDate] == NSOrderedDescending) {
//代表是1分钟之内
return @"刚刚";
}else{
//判断1小时之内
resultDate = [createDate dateByAddingTimeInterval:3600];
if ([resultDate compare:currentDate] == NSOrderedDescending) {
//代表1小时之内
//计算多少分钟前
NSTimeInterval interval = [currentDate timeIntervalSinceDate:createDate];
//分钟数
NSInteger intervalMinute = interval/60;
return [NSString stringWithFormat:@"%zd分钟前",intervalMinute];
}else{
//几小时前
NSTimeInterval interval = [currentDate timeIntervalSinceDate:createDate];
//分钟数
NSInteger intervalMinute = interval/3600;
return [NSString stringWithFormat:@"%zd小时前",intervalMinute];
}
}
//是不是1分钟之内
//是不是1小时之内
}else{
//如果是昨天-->昨天 11:11
if ([self isYesterdayWithDate:createDate]) {
//不是今天先这么去显示:08-01 15-03
formatter.dateFormat = @"昨天 HH:mm";
return [formatter stringFromDate:createDate];
}else{
//不是今天先这么去显示:08-01 15-03
formatter.dateFormat = @"MM-dd HH:mm";
return [formatter stringFromDate:createDate];
}
}
}else{
//不是今年:2014-05-05 09:17:31
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
return [formatter stringFromDate:createDate];
}
}
/**
* 是否是与当前时间是同一年
*
* @param date 传入对比的时间
*
* @return <#return value description#>
*/
- (BOOL)isThisYearWithDate:(NSDate *)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy";
//当前w创建的时间
NSDate *createDate = date;
//取出当前时间
NSDate *currentDate = [NSDate date];
//拿着创建时间与当前时间进行对比-->只格式化年份
formatter.dateFormat = @"yyyy";
//取出年份-->判断是否同一年,只要拿着年份进行对比一下,看一下是否一样
NSString *createDateYearStr = [formatter stringFromDate:createDate];
NSString *currentDateYearStr = [formatter stringFromDate:currentDate];
//对比年份的时间字符串,如果不是同一年的话返回NO
return [createDateYearStr isEqualToString:currentDateYearStr];
}
/**
* 判断与今天否是同一天,是否是今天
*
* @param date
*
* @return
*/
- (BOOL)isTodayWithDate:(NSDate *)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//拿着创建时间与当前时间进行对比-->只格式化年份-月份-天
formatter.dateFormat = @"yyyy-MM-dd";
//当前wei创建的时间
NSDate *createDate = date;
//取出当前时间
NSDate *currentDate = [NSDate date];
//取出年份-->判断是否是今天,
NSString *createDateYearStr = [formatter stringFromDate:createDate];
NSString *currentDateYearStr = [formatter stringFromDate:currentDate];
//对比年份的时间字符串,如果不是同一年的话返回NO
return [createDateYearStr isEqualToString:currentDateYearStr];
}
/**
* 与当前时间对比,判断是否是昨天
*
* @param date <#date description#>
*
* @return <#return value description#>
*/
- (BOOL)isYesterdayWithDate:(NSDate *)date{
//2014-09-30 10:00:00 创建时间
//2014-10-01 09:00:00 当前
NSDate *currentDate = [NSDate date];
//取出当前日历对象
NSCalendar *canlendar = [NSCalendar currentCalendar];
//NSCalendarUnit 对比单位-->要对比些什么东西
//设置对比对象
NSCalendarUnit unit = NSCalendarUnitYear|NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
//时间比较的结果
NSDateComponents *components = [canlendar components:unit fromDate:date toDate:currentDate options:NSCalendarWrapComponents];
//对比两个时间的day差值是否为
// NSLog(@"day=%zd,hour=%zd,minute=%zd,second=%zd",components.day,components.hour,components.minute,components.second);
return components.day == 1;
}
supersr--时间显示逻辑-->NSDate+NSCalendar
标签:
原文地址:http://www.cnblogs.com/supersr/p/4856527.html