获取对应月的第一天日期或者最后一天日期

//获取日期date对应月的第一天日期//获取日期date对应月的最后一天日期

+ (NSString *)getMonthFirstDayWithDate:(NSDate *)date format:(NSString *)aformat{

    NSDate *newDate = date;

    double interval = 0;

    NSDate *firstDate = nil;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];

    if (bl) {

        NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];

        [myDateFormatter setDateFormat:aformat];

        NSString *firstString = [myDateFormatter stringFromDate: firstDate];

        return firstString;

    }

    return @"";

}

+ (NSString *)getMonthLastDayWithDate:(NSDate *)date format:(NSString *)aformat{

    NSDate *newDate = date;

    double interval = 0;

    NSDate *firstDate = nil;

    NSDate *lastDate = nil;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];

    if (bl) {

        lastDate = [firstDate dateByAddingTimeInterval:interval - 1];

        NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];

        [myDateFormatter setDateFormat:aformat];

        NSString *lastString = [myDateFormatter stringFromDate: lastDate];

        return lastString;

    }

    return @"";

}

你可能感兴趣的:(获取对应月的第一天日期或者最后一天日期)