- (NSString*)weekDayStr:(NSString *)format { NSString *weekDayStr = nil; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSString *str = [self description]; if (str.length >= 10) { NSString *nowString = [str substringToIndex:10]; NSArray *array = [nowString componentsSeparatedByString:@"-"]; if (array.count == 0) { array = [nowString componentsSeparatedByString:@"/"]; } if (array.count >= 3) { int year = [[array objectAtIndex:0] integerValue]; int month = [[array objectAtIndex:1] integerValue]; int day = [[array objectAtIndex:2] integerValue]; [comps setYear:year]; [comps setMonth:month]; [comps setDay:day]; } } NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *_date = [gregorian dateFromComponents:comps]; NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:_date]; int week = [weekdayComponents weekday]; week ++; switch (week) { case 1: weekDayStr = @"星期日"; break; case 2: weekDayStr = @"星期一"; break; case 3: weekDayStr = @"星期二"; break; case 4: weekDayStr = @"星期三"; break; case 5: weekDayStr = @"星期四"; break; case 6: weekDayStr = @"星期五"; break; case 7: weekDayStr = @"星期六"; break; default: weekDayStr = @""; break; } return weekDayStr; }
时间: 2024-11-23 14:08:29