如何获得一个月的周数

时间:2013-03-06 09:28:32

标签: iphone ios objective-c ios5 ios6

这是问题,我正在寻找一种方法来获得一个月的周数。我已经找到了一个似乎适用于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)是英文。也许它可以改变一些东西(比如一周的第一天?)?

2 个答案:

答案 0 :(得分:16)

试试这个:

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

Swift 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

答案 1 :(得分: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类别将返回同一周内任何日期的工作日。

最诚挚的问候。