iOS 计算某个月的天数 计算某天的星期

// 某年某月的天数
- (NSInteger)dayCount:(NSInteger)years
{
    NSInteger count = 0;
    for (int i = 1; i <= 12; i++) {
        if (2 == i) {
            if((years % 4 == 0 && years % 100!=0) || years % 400 == 0) //是闰年
            {
                count = 29;
            }
            else
            {
                count = 28;
            }
            
        }else if (4 == i || 6 == i || 9 == i || 11 == i){
            count = 30;
        }else{
            count = 31;
        }
    }
    return count;
}
// 某天的星期
- (NSString * )whichWeek:(NSString *)dateStr
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate* inputDate = [inputFormatter dateFromString:dateStr];
    
    NSDateComponents *dateComps = [calendar components:NSCalendarUnitWeekday fromDate:inputDate];
    
    NSInteger weekDay = dateComps.weekday;
    
    switch (weekDay) {
        case 1:
            return @"星期日";
            break;
        case 2:
            return @"星期一";
            break;
        case 3:
            return @"星期二";
            break;
        case 4:
            return @"星期三";
            break;
        case 5:
            return @"星期四";
            break;
        case 6:
            return @"星期五";
            break;
        case 7:
            return @"星期六";
            break;
        default:
            return @"";
            break;
    }
}

 

iOS 计算某个月的天数 计算某天的星期,布布扣,bubuko.com

iOS 计算某个月的天数 计算某天的星期

上一篇:android中正确导入第三方jar包


下一篇:瑞柏匡丞告诉你移动如何改变生活