OC获取月份的最后一天

最近项目中用到这个,这个方法可以返回任意一个年份中月份的最后一天

+(NSString *)getMonthLastDay:(int)year month:(int )month
{
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd"];
    if (month == 12) {
        month = 0;
        year++;
    }
    NSDate *targetDate = [format dateFromString:[NSString stringWithFormat:@"%04d-%02d-01",year,month+1]];
    
    NSDate *orgDate = [NSDate dateWithTimeInterval:(24*60*60)*(-1) sinceDate:targetDate];
    
    NSString *str = [format stringFromDate:orgDate];
    
    return str;
}

推荐阅读更多精彩内容