13

这是问题所在,我正在寻找一种方法来获取一个月中的周数。我已经找到了一个似乎适用于 ios 5 的解决方案,但它不适用于 io6(返回的数字不一样,在 ios 6 上还有一个)。

- (int)weeksOfMonth:(int)month inYear:(int)year
{
    NSCalendar *cCalendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setMonth:month];
    [components setYear:year];

    NSRange range = [cCalendar rangeOfUnit:NSDayCalendarUnit
                               inUnit:NSMonthCalendarUnit
                              forDate:[cCalendar dateFromComponents:components]];

    cCalendar = [NSCalendar currentCalendar];
    [cCalendar setMinimumDaysInFirstWeek:4];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier:@"fr"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSMutableSet *weeks = [[NSMutableSet alloc] init ];

    for(int i = 0; i < range.length; i++)
    {
        NSString *temp = [NSString stringWithFormat:@"%4d-%2d-   %2d",year,month,range.location+i];
        NSDate *date = [dateFormatter dateFromString:temp ];
        int week = [[cCalendar components: NSWeekOfYearCalendarUnit fromDate:date] weekOfYear];
        [weeks addObject:[NSNumber numberWithInt:week]];
    }

    return [weeks count];
}

io6 上返回值为 6,io5 上返回值为 5。

你有什么主意吗 ?

编辑:另一件事,我的设备(ios5)是法语,模拟器(ios6)是英语。也许它可以改变一些东西(比如一周的第一天?)?

4

2 回答 2

17

试试这个:

NSDate *date = [NSDate date];//since you are forming date, put it here
NSCalendar *calender = [NSCalendar currentCalendar];
NSRange weekRange = [calender rangeOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
NSInteger weeksCount=weekRange.length;
NSLog(@"%d",weeksCount);

斯威夫特 4.0:

let date = Date()
let calendar = Calendar.current
let weekRange = calendar.range(of: .weekOfMonth, 
                               in: .month, 
                               for: date)
let weeksCount = weekRange.count ?? 0
print(weeksCount)

或将您的方法更改为:

- (NSInteger)weeksOfMonth:(int)month inYear:(int)year{
    NSString *dateString=[NSString stringWithFormat:@"%4d/%d/1",year,month];

    NSDateFormatter *dfMMddyyyy=[NSDateFormatter new];
    [dfMMddyyyy setDateFormat:@"yyyy/MM/dd"];
    NSDate *date=[dfMMddyyyy dateFromString:dateString];

    NSCalendar *calender = [NSCalendar currentCalendar];
    NSRange weekRange = [calender rangeOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
    NSInteger weeksCount=weekRange.length;

    return weeksCount;
}

编辑:

在上述方法中使用它

 //NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 [calender setFirstWeekday:2]; //sunday=1, monday=2 etc
于 2013-03-06T09:40:35.910 回答
0

检查这种方法。

-(NSArray*)getWeekDaysForDate {


int dayOfWeek = [self weekDay];

NSMutableArray *array = [NSMutableArray array];

NSDate *weekStartDate = [self offsetDay:1-dayOfWeek];

NSCalendar *calendar = [NSCalendar defaultCalendar];
NSInteger monthValue = [self month];
unsigned int weekNum = [[calendar components: NSWeekCalendarUnit
                               fromDate: weekStartDate] week];
NSDate * nextDate = weekStartDate, *curDate;
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
unsigned int newWeekNum;
do {
    curDate = nextDate;
    nextDate = [calendar dateByAddingComponents:oneDay toDate: curDate
                                   options:0];

    newWeekNum = [[calendar components: NSWeekCalendarUnit fromDate:
                   nextDate] week];
    int monthValueForNextDate = [[calendar components: NSMonthCalendarUnit fromDate:
                       nextDate] month];

    if(monthValue == monthValueForNextDate)
        [array addObject:nextDate]];

} while (newWeekNum == weekNum);

[array sortUsingSelector:@selector(compare:)];

return array;
}

-(int)month {
   NSCalendar *gregorian = [NSCalendar defaultCalendar];
   NSDateComponents *components = [gregorian components:NSMonthCalendarUnit fromDate:self];
   return [components month];
}

-(int)weekDay {
   NSCalendar *gregorian = [NSCalendar defaultCalendar];
   NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit fromDate:self];
   return [components weekday];
}

-(NSDate *)offsetDay:(int)numDays {
   NSCalendar *gregorian = [NSCalendar defaultCalendar];
   NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
   [offsetComponents setDay:numDays];
   return [gregorian dateByAddingComponents:offsetComponents
                                  toDate:self options:0];
}

PS:NSCalendar defaultCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

添加此方法后,类别NSDate将返回同一周内任何日期的工作日。

此致。

于 2013-03-06T10:20:03.213 回答