2016-07-26 58 views
1

中獲取weekOfMonth關於從日期獲取WeekOfMonth有一點小問題。這是一個示例代碼。iOS:從日期問題

NSDateFormatter *dateformate=[[NSDateFormatter alloc]init]; 
[dateformate setDateFormat:@"dd/MM/yyyy"]; // Date formater 

NSDate *dateTest1 = [dateformate dateFromString:@"25/07/2016"]; //Week no: 5 
NSDate *dateTest2 = [dateformate dateFromString:@"01/08/2016"]; //Week no: 1 
NSDate *dateTest3 = [dateformate dateFromString:@"08/08/2016"]; //Week no: 2 
NSDate *dateTest4 = [dateformate dateFromString:@"15/08/2016"]; //Week no: 3 
NSDate *dateTest5 = [dateformate dateFromString:@"22/08/2016"]; //Week no: 4 
NSDate *dateTest6 = [dateformate dateFromString:@"29/08/2016"]; //Week no: 5 
NSDate *dateTest7 = [dateformate dateFromString:@"05/09/2016"]; //Week no: 6 

components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:dateTest1]; 
dateString = [dateformate stringFromDate:dateTest1]; 
NSLog(@"dateTest1 -> %@",dateString); //output : dateTest1 -> 25/07/2016 
NSLog(@"Week no: %ld",(long)[components weekOfMonth]); //output : Week no: 4 

components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:dateTest2]; 
dateString = [dateformate stringFromDate:dateTest2]; 
NSLog(@"dateTest2 -> %@",dateString); //output : dateTest2 -> 01/08/2016 
NSLog(@"Week no: %ld",(long)[components weekOfMonth]); //output : Week no: 1 

components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:dateTest3]; 
dateString = [dateformate stringFromDate:dateTest3]; 
NSLog(@"dateTest3 -> %@",dateString); //output : dateTest3 -> 08/08/2016 
NSLog(@"Week no: %ld",(long)[components weekOfMonth]); //output : Week no: 2 

components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:dateTest4]; 
dateString = [dateformate stringFromDate:dateTest4]; 
NSLog(@"dateTest4 -> %@",dateString); //output : dateTest4 -> 15/08/2016 
NSLog(@"Week no: %ld",(long)[components weekOfMonth]); //output : Week no: 3 

components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:dateTest5]; 
dateString = [dateformate stringFromDate:dateTest5]; 
NSLog(@"dateTest5 -> %@",dateString); //output : dateTest5 -> 22/08/2016 
NSLog(@"Week no: %ld",(long)[components weekOfMonth]); //output : Week no: 4 

components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:dateTest6]; 
dateString = [dateformate stringFromDate:dateTest6]; 
NSLog(@"dateTest6 -> %@",dateString); //output : dateTest6 -> 29/08/2016 
NSLog(@"Week no: %ld",(long)[components weekOfMonth]); //output : Week no: 5 

components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:dateTest7]; 
dateString = [dateformate stringFromDate:dateTest7]; 
NSLog(@"dateTest7 -> %@",dateString); //output : dateTest7 -> 05/09/2016 
NSLog(@"Week no: %ld",(long)[components weekOfMonth]); //output : Week no: 2 

正如你可以看到dateTest1應該Week no = 5和NOT Week no = 4。對於其他日期,我看到並且如你所見,沒有任何問題。

我需要正確的WeekOfMonth設置我FFCalendar正確 https://github.com/fggeraissate/FFCalendar

回答

2
let calendar = NSCalendar.currentCalendar() 
let dateComponent = calendar.components([.WeekOfYear, .Day, .Month, .Year], fromDate: NSDate(timeIntervalSinceNow: 0)) 
print("weekOfYear \(dateComponent.weekOfYear)") 

使用上面的代碼,你可以得到WEEKOFYEAR。現在您需要計算當月第一個日期的星期幾和您需要查找的日期。 現在得到weekOfMonth獲得的兩個WEEKOFYEAR

+0

感謝您的回答。使用weekOfMonth不依賴於我,它依賴於FFCalendar。在方法dateChanged上的FFWeekCalendarView中,我需要正確設置WeekOfMonth,因爲它適用於WeekOfMonth。我不認爲我可以在FFCalendar中使用weekOfYear而不是weekOfMonth。 – Bruno

+0

對不起,現在我明白了!它工作得很好。謝謝 – Bruno

+0

一週中的一週返回1-4或第五和第一週應該返回1-52或53 –

0

的DIFF + 1之後提出的解決方案拉吉 - aggrawal我做了這個解決方案,工作好

NSDate *date = [[FFDateManager sharedManager] currentDate]; 

NSDateFormatter *dateformate=[[NSDateFormatter alloc]init]; 
[dateformate setDateFormat:@"dd/MM/yyyy"]; // Date formater 

NSString *dateString = [dateformate stringFromDate:date]; 
NSCalendar* calendar = [NSCalendar currentCalendar]; 
NSDateComponents* dateComponent = [calendar components:NSWeekOfYearCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date]; 
NSInteger weekOfYearCurrent =(long)[dateComponent weekOfYear]; 
dateString = [dateformate stringFromDate:date]; 
NSLog(@"dateCurrent -> %@",dateString); 
NSLog(@"weekOfYearCurrent: %ld",(long)weekOfYearCurrent); 

NSDate *dateFirstMonth = [dateformate dateFromString:[NSString stringWithFormat:@"01/%d/2016", (int)[dateComponent month]]]; 
dateComponent = [calendar components:NSWeekOfYearCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:dateFirstMonth]; 
NSInteger weekOfYearFirstDayOfMonth =(long)[dateComponent weekOfYear]; 
dateString = [dateformate stringFromDate:dateFirstMonth]; 
NSLog(@"dateFirstMonth -> %@",dateString); 
NSLog(@"weekOfYearFirstDayOfMonth: %ld",(long)weekOfYearFirstDayOfMonth); 

NSInteger NumberWeekOfMonth = (weekOfYearCurrent - weekOfYearFirstDayOfMonth) + 1; 
NSLog(@"NumberWeekOfMonth: %ld",(long)NumberWeekOfMonth);