• ios开发日期的NSDate,NSCalendar分类


    #import <Foundation/Foundation.h>
    
    @interface NSDate (XMGExtension)
    /**  */
    // @property (nonatomic, copy) NSString *name;
    //- (NSString *)name;
    //- (void)setName:(NSString *)name;
    
    /**
     *  是否为今年
     */
    - (BOOL)isThisYear;
    
    /**
     *  是否为今天
     */
    - (BOOL)isToday;
    
    /**
     *  是否为昨天
     */
    - (BOOL)isYesterday;
    
    /**
     *  是否为明天
     */
    - (BOOL)isTomorrow;
    
    @end
    #import "NSDate+XMGExtension.h"
    
    @implementation NSDate (XMGExtension)
    
    - (BOOL)isThisYear
    {
        // 判断self这个日期是否为今年
        NSCalendar *calendar = [NSCalendar calendar];
        
        //
        NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
        NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
        
        return selfYear == nowYear;
    }
    //- (BOOL)isThisYear
    //{
    //    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    //    fmt.dateFormat = @"yyyy";
    //    
    //    ////    NSString *selfYear = [fmt stringFromDate:self];
    //    NSString *nowYear = [fmt stringFromDate:[NSDate date]];
    //    
    //    return [selfYear isEqualToString:nowYear];
    //}
    - (BOOL)isToday
    {
        // 判断self这个日期是否为今天
        NSCalendar *calendar = [NSCalendar calendar];
        
        // 获得年月日元素
        NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
        NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
        
        return selfCmps.year == nowCmps.year
        && selfCmps.month == nowCmps.month
        && selfCmps.day == nowCmps.day;
    }
    
    //- (BOOL)isToday
    //{
    //    // 判断self这个日期是否为今天
    //    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    //    fmt.dateFormat = @"yyyyMMdd";
    //
    //    NSString *selfString = [fmt stringFromDate:self];
    //    NSString *nowString = [fmt stringFromDate:[NSDate date]];
    //
    //    return [selfString isEqualToString:nowString];
    //}
    
    - (BOOL)isYesterday
    {
        // 判断self这个日期是否为昨天
        
        // self == 2015-10-31 23:07:08 -> 2015-10-31 00:00:00
        // now  == 2015-11-01 14:39:20 -> 2015-11-01 00:00:00
        
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyyMMdd";
        
        NSString *selfString = [fmt stringFromDate:self]; // 20151031
        NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20151101
        
        NSDate *selfDate = [fmt dateFromString:selfString]; // 2015-10-31 00:00:00
        NSDate *nowDate = [fmt dateFromString:nowString]; // 2015-11-01 00:00:00
        
        NSCalendar *calendar = [NSCalendar calendar];
        NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
        
        return cmps.year == 0
        && cmps.month == 0
        && cmps.day == 1;
    }
    
    - (BOOL)isTomorrow
    {
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyyMMdd";
        
        NSString *selfString = [fmt stringFromDate:self]; // 20151031
        NSString *nowString = [fmt stringFromDate:[NSDate date]]; // 20151101
        
        NSDate *selfDate = [fmt dateFromString:selfString]; // 2015-10-31 00:00:00
        NSDate *nowDate = [fmt dateFromString:nowString]; // 2015-11-01 00:00:00
        
        NSCalendar *calendar = [NSCalendar calendar];
        NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
        
        return cmps.year == 0
        && cmps.month == 0
        && cmps.day == -1;
    }
    #import <Foundation/Foundation.h>
    
    @interface NSCalendar (XMGExtension)
    + (instancetype)calendar;
    @end
    #import "NSCalendar+XMGExtension.h"
    
    @implementation  (XMGExtension)
    + (instancetype)calendar
    {
        if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
            return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        } else {
            return [NSCalendar currentCalendar];
        }
    
    }
    @end
  • 相关阅读:
    [HEOI2015]兔子与樱花
    [HNOI2015]亚瑟王
    [JSOI2011]分特产
    某考试 T3 sine
    [JSOI2015]最小表示
    51NOD 1258 序列求和 V4
    Codeforces 622F The Sum of the k-th Powers
    Loj #6261. 一个人的高三楼
    [HAOI????] 硬币购物
    bzoj4318 OSU!
  • 原文地址:https://www.cnblogs.com/cqb-learner/p/5774475.html
Copyright © 2020-2023  润新知