Ios7 iOS:DatePicker选择器显示确切的值,但返回错误的日期

Ios7 iOS:DatePicker选择器显示确切的值,但返回错误的日期,ios7,datepicker,Ios7,Datepicker,我以编程方式为iOS7创建了一个日期选择器,并设置了最大和最小日期。但对于某些特定的12月份,它返回了错误的年份 [fromDatePicker addTarget:self action:@selector(fromDatePickerAction:) forControlEvents:UIControlEventValueChanged]; NSDate *date = [NSDate date]; NSCalendar *gregorian = [[NSCalend

我以编程方式为iOS7创建了一个日期选择器,并设置了最大和最小日期。但对于某些特定的12月份,它返回了错误的年份

    [fromDatePicker addTarget:self action:@selector(fromDatePickerAction:) forControlEvents:UIControlEventValueChanged];
    NSDate *date = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents * components = [[NSDateComponents alloc] init];
    NSDateComponents *monthComponents = [gregorian components:NSMonthCalendarUnit fromDate:date];
    int currentMonth = [monthComponents month];
    NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:date];
    int dayss=[dayComponents day];
    [components setYear:0];
    NSDate * minDate = [gregorian dateByAddingComponents: components toDate: date options: 0];
    [components setYear:0];
    [components setMonth:12-currentMonth];
    [components setDay:31-dayss];
    NSDate * maxDate = [gregorian dateByAddingComponents: components toDate: date options: 0];
    fromDatePicker.minimumDate=minDate;
    fromDatePicker.maximumDate=maxDate;
    [self.view addSubview:fromDatePicker];

- (void) fromDatePickerAction:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm aa"];
[self.delegate didTouchAddNotePicker:[dateFormatter stringFromDate:[fromDatePicker date]]];
NSLog(@"%@",[dateFormatter stringFromDate:[fromDatePicker date]]);
//_lblDate.text=[dateFormatter stringFromDate:[fromDatePicker date]];
}

在这里,我选择图1所示的日期,但它返回图2中的日期


请帮助解决此问题

此问题通过在日期选择器方法中将[dateFormatter setDateFormat:@“YYYY MM dd hh:MM aa”]更改为[dateFormatter setDateFormat:@“YYYY MM dd hh:MM aa”]来解决


你真是个救世主!非常感谢这个人!节省了我们搜索和调试的时间!