中獲取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
感謝您的回答。使用weekOfMonth不依賴於我,它依賴於FFCalendar。在方法dateChanged上的FFWeekCalendarView中,我需要正確設置WeekOfMonth,因爲它適用於WeekOfMonth。我不認爲我可以在FFCalendar中使用weekOfYear而不是weekOfMonth。 – Bruno
對不起,現在我明白了!它工作得很好。謝謝 – Bruno
一週中的一週返回1-4或第五和第一週應該返回1-52或53 –