iOS 根据年月获取月份天数

// 根据年和月获取月份天数

-(NSInteger)monthDayWithYear:(NSInteger) year month:(NSInteger) month{

    // 字符串转日期

    NSString *dateStr = [NSString stringWithFormat:@"%ld-%ld-05 00:00:00",year,month];

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

    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    NSDate *date = [format dateFromString:dateStr];

    // 当前日历

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];

    return range.length;

}

posted on 2020-01-15 20:50  许鸣  阅读(617)  评论(0编辑  收藏  举报