1

我想计算给定月份的周数。例如,2012 年 7 月应该有第 26、27、28、29、30 和 31 周。我制作了这个片段,但我正在寻找一种可行的方法来获取前一周和后一周的数字。有任何想法吗。

谢谢,达雷尔。

//Get current week number
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:[NSDate date]];
NSInteger week = [components week];
NSLog(@"Week nummer: %d", (int)week);

//Get week numbers in month
[components weekOfYear];
NSRange weeksInMonth = [cal rangeOfUnit:NSWeekCalendarUnit
                          inUnit:NSMonthCalendarUnit
                         forDate:[cal dateFromComponents:components]];
NSLog(@"Weken in maand: %lu", weeksInMonth.length);
4

3 回答 3

4

功能:

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

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

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

    calendar = [NSCalendar currentCalendar];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [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 = [[calendar components: NSWeekOfYearCalendarUnit fromDate:date] weekOfYear];
        [weeks addObject:[NSNumber numberWithInt:week]];
    }

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
    NSArray *descriptors = [[NSArray alloc] initWithObjects:descriptor, nil];
    return [weeks sortedArrayUsingDescriptors:descriptors];
}

用法:

NSArray *weeks = [self weeksOfMonth:7 inYear:2012];
NSLog(@"%@",weeks);

输出:

{(
    27,
    28,
    29,
    30,
    31
)}
于 2012-07-12T21:22:22.063 回答
2
int year = 2012;
int month = 7;

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *date = nil;
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];

[components setYear:year];
[components setMonth:month];
date = [cal dateFromComponents:components];
NSLog(@"starting week # %d = %@", [[cal components:NSWeekCalendarUnit fromDate:date] week], date);

[components setMonth:month + 1];
[components setDay:-1];
date = [cal dateFromComponents:components];
NSLog(@"ending week # %d = %@", [[cal components:NSWeekCalendarUnit fromDate:date] week], date);
于 2012-07-12T20:45:50.790 回答
1

安妮建议的解决方案对我有用,但输出不像她的输出那样排序。我需要转换为数组并排序如下:

// Sorting everything.
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *weeks = [[self weeksOfMonth:7 inYear:2012]allObjects];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
NSArray *sortedArray = [weeks sortedArrayUsingDescriptors:sorters];

for (NSNumber *element in sortedArray)
{
    NSLog(@"%i",element.intValue);
} 

这是合乎逻辑的步骤,是否有更性感的方式来做到这一点?

于 2012-07-19T19:58:58.503 回答