oc_Date

+ (NSInteger)day:(NSDate *)date{
        NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
        return [components day];
    }

    + (NSInteger)month:(NSDate *)date{
        NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
        return [components month];
    }

    + (NSInteger)year:(NSDate *)date{
        NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
        return [components year];
    }

    //找到,一周第一天是周几
    + (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        [calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
        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;
    }

    + (NSInteger)totaldaysInMonth:(NSDate *)date{
        NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
        return daysInLastMonth.length;
    }

    + (NSDate *)lastMonth:(NSDate *)date{
        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.month = -1;
        NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
        return newDate;
    }

    + (NSDate*)nextMonth:(NSDate *)date{
        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.month = +1;
        NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
        return newDate;
    }

    + (NSDate *)lastDay:(NSDate *)date {
        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.day = -1;
        NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
        return newDate;
    }

    + (NSDate *)nextDay:(NSDate *)date; {
        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.day = +1;
        NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
        return newDate;
    }

    + (NSString *)getStringOfCurrentDate:(NSDate *)date{
        
        NSString *dateString = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)[HWDateHelper year:date],(long)[HWDateHelper month:date],(long)[HWDateHelper day:date]];
        
        return dateString;
    }

你可能感兴趣的:(oc_Date)