计算给定月份的周数

时间:2012-07-12 20:17:22

标签: objective-c xcode nscalendar nsdatecomponents

我想计算给定月份的周数。 例如,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);

3 个答案:

答案 0 :(得分: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
)}

答案 1 :(得分: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);

答案 2 :(得分:1)

Anne sugested的解决方案适用于我,但输出并不像她的输出那样排序。 我需要转换为数组并排序如下:

// 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);
} 

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