2013-03-06 93 views
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)是英文。也許它可以改變一些東西(比如每週的第一天?)?

+0

即使在osx中​​它說6.這是正確的計數。 – 2013-03-06 09:35:40

+0

你正在試圖顯示問題的月份和年份是多少? – occulus 2013-03-06 09:41:14

+0

檢查我的updatd代碼,我檢查它在osx和ios6中,它工作正常。 – 2013-03-06 10:40:35

回答

15

試試這個:

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 
+0

謝謝你的代碼,但結果不會改變,我仍然有5在我的設備上(ios5)和6在模擬器上(ios6)。 我忘了一件事,也許這是一個本地化問題,也許在法國(像我的設備一樣)星期一開始星期一(我想要的),但不是英文? – 2013-03-06 18:04:35

+0

你走了,這是問題所在,不過你上面有一個較短的解決方案。 – 2013-03-06 18:06:23

+0

檢查我編輯的答案; – 2013-03-06 18:09:57

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類將返回位於同一周的任何日期的工作日。

最好的問候。

相關問題