美文网首页iOS-Developer-OC111iOS
iOS分享干货-项目开发中常用的时间日期分类

iOS分享干货-项目开发中常用的时间日期分类

作者: 船长_ | 来源:发表于2015-11-21 22:06 被阅读637次
    • 场景需求:从网络请求返回的时间数据一般是时间字符串,比如@"2015-10-31 23:07:08"对这个时间字符串进行判断,截取,转换成我们想要的格式;比如
    时间字符串操作要求.png
    #import <Foundation/Foundation.h>
    @interface NSDate (DXExtension)
    - (BOOL)isThisYear;
    - (BOOL)isToday;
    - (BOOL)isYesterday;
    - (BOOL)isInOneMinute;
    - (BOOL)isInOneHour;
    - (BOOL)isTomorrow;
    @end
    
    @implementation NSDate (DXExtension)
    
    static NSDateFormatter *formatter_;
    static  NSCalendar *calendar_;
    
    // 第一次使用这个类的时候调用
    // 优化性能,减少频繁的创建格式化对象
    + (void)initialize
    {
        formatter_ = [[NSDateFormatter alloc] init];
        calendar_ = [NSCalendar calendar];
    }
    - (BOOL)isToday
    {
        formatter_.dateFormat = @"yyyyMMdd";
        
        NSString *selfDay = [formatter_ stringFromDate:self];
        NSString *nowDay = [formatter_ stringFromDate:[NSDate date]];
        
        return [selfDay isEqualToString:nowDay];
    }
    
    - (BOOL)isYesterday
    {
        formatter_.dateFormat = @"yyyyMMdd";
        
        NSString *selfDay = [formatter_ stringFromDate:self];
        NSString *nowDay = [formatter_ stringFromDate:[NSDate date]];
        
        NSDate *selfDate = [formatter_ dateFromString:selfDay];
        NSDate *nowDate = [formatter_ dateFromString:nowDay];
        
        NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents *cmp = [calendar_ components:units fromDate:nowDate toDate:selfDate options:0];
        
        return cmp.year == 0 && cmp.month == 0 && cmp.day == 1;
    }
    
    - (BOOL)isTomorrow
    {
        formatter_.dateFormat = @"yyyyMMdd";
        
        NSString *selfDay = [formatter_ stringFromDate:self];
        NSString *nowDay = [formatter_ stringFromDate:[NSDate date]];
        
        NSDate *selfDate = [formatter_ dateFromString:selfDay];
        NSDate *nowDate = [formatter_ dateFromString:nowDay];
        
        NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents *cmp = [calendar_ components:units fromDate:nowDate toDate:selfDate options:0];
        
        return cmp.year == 0 && cmp.month == 0 && cmp.day == -1;
    }
    
    - (BOOL)isThisYear
    {
        formatter_.dateFormat = @"yyyy";
        NSString *selfYear = [formatter_ stringFromDate:self];
        NSString *currentYear = [formatter_ stringFromDate:[NSDate date]];
        
        return [selfYear isEqualToString:currentYear];
    }
    - (BOOL)isInOneMinute
    {
        formatter_.dateFormat = @"yyyyMMddHHmm";
        
        NSString *selfSecond = [formatter_ stringFromDate:self];
        NSString *nowSecond = [formatter_ stringFromDate:[NSDate date]];
        
        return [selfSecond isEqualToString:nowSecond];
    }
    
    - (BOOL)isInOneHour
    {
        formatter_.dateFormat = @"yyyyMMddHH";
        
        NSString *selfHour = [formatter_ stringFromDate:self];
        NSString *nowHour = [formatter_ stringFromDate:[NSDate date]];
        
        return [selfHour isEqualToString:nowHour];
        
    }
    @end
    

    相关文章

      网友评论

      本文标题:iOS分享干货-项目开发中常用的时间日期分类

      本文链接:https://www.haomeiwen.com/subject/chtrhttx.html