码迷,mamicode.com
首页 > 其他好文 > 详细

新浪微博客户端(28)-格式化日期显示

时间:2016-11-21 22:34:30      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:port   mat   客户端   sdi   equal   att   ict   object   格式   

 

DJStatus.m

#import "DJStatus.h"
#import "MJExtension.h"
#import "DJPhoto.h"

@implementation DJStatus


+ (NSDictionary *)mj_objectClassInArray {

    return @{@"pic_urls":[DJPhoto class]};

}



// 格式化返回日期
- (NSString *)created_at {

    
    /* 将日期字符串转换为NSDate对象 */
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    NSDate *createDate = [fmt dateFromString:_created_at];
    
    NSDate *nowDate = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    // 获取两个指定日期之前的差值
    NSDateComponents *components = [calendar components:unit fromDate:createDate toDate:nowDate options:0];
  
    
    if ([createDate isThisYear]) { // 今年
        if ([createDate isYesterday]) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm";
            return [fmt stringFromDate:createDate];
        } else if ([createDate isToday]) { // 今天
            
            if (components.hour >= 1) { // 大于1小时
                return [NSString stringWithFormat:@"%zd小时前",components.hour];
            } else if (components.minute >= 1) { // 1小时内
                return [NSString stringWithFormat:@"%zd分钟前",components.minute];
            } else { // 1分钟内
                return @"刚刚";
            }
        
        } else { // 今年的其它天
            fmt.dateFormat = @"MM-dd HH:mm";
            return [fmt stringFromDate:createDate];
        }
        
    } else { // 非今年
        fmt.dateFormat = @"yyyy-MM-dd HH:mm";
        return [fmt stringFromDate:createDate];
    }
    
    return _created_at;

}

@end

NSDate+Extension.m

#import "NSDate+Extension.h"

@implementation NSDate (Extension)


/** 判断当前日期是否是今年 */
- (BOOL)isThisYear {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit yearUnit = NSCalendarUnitYear;
    
    NSDate *nowDate = [NSDate date];
    
    NSDateComponents *createDateComponents = [calendar components:yearUnit fromDate:self];
    NSDateComponents *nowDateComponents = [calendar components:yearUnit fromDate:nowDate];
    
    return createDateComponents.year == nowDateComponents.year;
    
}




/** 判断当前日期是否是昨天 */
- (BOOL)isYesterday {
    
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    NSString *createDateStr = [fmt stringFromDate:self];
    NSString *nowDateStr = [fmt stringFromDate:nowDate];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *fmtCreateDate = [fmt dateFromString:createDateStr];
    NSDate *fmtNowDate = [fmt dateFromString:nowDateStr];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    
    NSDateComponents *components = [calendar components:unit fromDate:fmtCreateDate toDate:fmtNowDate options:0];
    
    // 要求年份差值为0,月份差值为0,天数差值为1天
    return (components.year == 0) && (components.month == 0) && (components.day == 1) ;
    
}




/** 判断当前日期是否是同一天 */
- (BOOL)isToday {
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    NSString *createDateStr = [fmt stringFromDate:self];
    NSString *nowDateStr = [fmt stringFromDate:nowDate];
    
    return [createDateStr isEqualToString:nowDateStr];
    
}




@end

 

最终效果:

技术分享

 

新浪微博客户端(28)-格式化日期显示

标签:port   mat   客户端   sdi   equal   att   ict   object   格式   

原文地址:http://www.cnblogs.com/yongdaimi/p/6087120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!