美文网首页常用方法程序员iOS Developer
整理关于NSCalendar常用知识点

整理关于NSCalendar常用知识点

作者: 小兵快跑 | 来源:发表于2017-03-30 00:10 被阅读150次

    小兵项目中用到日历签到功能,之前写过相关的Demo,从新整理一遍,温故而知新,重新整理了份相关知识点

    .h文件方法函数
    @interface XFCalendarTool : NSObject
    
    + (NSInteger)day:(NSDate *)date;
    + (NSInteger)month:(NSDate *)date;
    + (NSInteger)year:(NSDate *)date;
    
    + (NSInteger)firstWeekdayInThisMonth:(NSDate *)date;
    + (NSInteger)totaldaysInMonth:(NSDate *)date;
    
    + (NSDate *)lastMonth:(NSDate *)date;
    + (NSDate*)nextMonth:(NSDate *)date;
    
    
    @implementation XFCalendarTool
    
    /**
    
     @param date 当前 几号
     @return <#return value description#>
     */
    + (NSInteger)day:(NSDate *)date{
        NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitDay) fromDate:date];
        return [components day];
    }
    
    
    /**
     当前 月
    
     @param date <#date description#>
     @return <#return value description#>
     */
    + (NSInteger)month:(NSDate *)date{
        NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitMonth) fromDate:date];
        return [components month];
    }
    /**
     当前 年
     
     @param date <#date description#>
     @return <#return value description#>
     */
    + (NSInteger)year:(NSDate *)date{
        NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear) fromDate:date];
        return [components year];
    }
    
    
    /**
     当前时间 当前月 1号 是 weak 
    
     @param date <#date description#>
     @return <#return value description#>
     */
    + (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        [calendar setFirstWeekday:1];
        NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
        [comp setDay:1];
        NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
        NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
        return firstWeekday - 1;
    }
    
    
    /**
     当前月 天数
    
     @param date 31-
     @return <#return value description#>
     */
    + (NSInteger)totaldaysInMonth:(NSDate *)date{
        NSRange daysInOfMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
        return daysInOfMonth.length;
    }
    
    /**
     上个月 
    
     @param date 2017-02-28 14:29:50 +0000--
     @return <#return value description#>
     */
    + (NSDate *)lastMonth:(NSDate *)date{
        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.month = -1;
        NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
        return newDate;
    }
    
    
    /**
     下个月 
    
     @param date 2017-04-29 14:29:50 +0000--
     @return <#return value description#>
     */
    + (NSDate*)nextMonth:(NSDate *)date{
        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.month = +1;
        NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
        return newDate;
    }
    
    
    
    
    
    

    Calendar


    关注我

    QQ -- iOS/Swift 交流群:107548668

    相关文章

      网友评论

        本文标题:整理关于NSCalendar常用知识点

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