iOS:从日期问题开始获取weekOfMonth

时间:2016-07-26 07:40:10

标签: ios xcode calendar nsdate ios9.2

从日期开始获取WeekOfMonth有一点问题。这是一个示例代码。

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而不是Week no = 4。对于其他日期,我看到并且你可以看到没有任何问题。

我需要正确的WeekOfMonth来正确设置我的FFCalendar https://github.com/fggeraissate/FFCalendar

2 个答案:

答案 0 :(得分:2)

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

使用上面的代码,您可以获得weekOfYear。现在,您需要计算该月第一个日期的weekOfYear和您需要查找的日期。 现在获取weekOfMonth得到WeekOfYear的差异+ 1

答案 1 :(得分:0)

根据提出的解决方案raj-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);