iOS 计算指定日期是周几星期几

返回的直接是星期一到日,如有需要把星期修改为周即可。

+ (NSString *)dateisweek:(NSDate *)date {
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:kCFCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];
    NSString *string;
    switch (comps.weekday) {
        case 1:
            string = @"星期天";
            break;
        case 2:
            string = @"星期一";
            break;
        case 3:
            string = @"星期二";
            break;
        case 4:
            string = @"星期三";
            break;
        case 5:
            string = @"星期四";
            break;
        case 6:
            string = @"星期五";
            break;
        case 7:
            string = @"星期六";
            break;
            
        default:
            break;
    }
    return string;
}

 

你可能感兴趣的:(iOS,开发小技巧)