我有 2 个日期选择器,每个选择器都遵循以太(280 天),选择器正确更改,但是mydatepicker.date
当 NSlog 或将其放入UITextfield
日期时变为日期 - 1 天或日期 + 1 天
这是我的代码:
第一个选择器:
int daysToAdd = -280;
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];
// create a calendar
NSCalendar *addingdays = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate = [addingdays dateByAddingComponents:components toDate:dueDatePickerView.date options:0];
lastPerPickerView.date = newDate;
NSLog(@"%@",newDate);
第二个:
int daysToAdd2 = 280;
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd2];
// create a calendar
NSCalendar *addingdays2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate2 = [addingdays2 dateByAddingComponents:components toDate:lastPerPickerView.date options:0];
dueDatePickerView.date = newDate2;
NSLog(@"%@",newDate2);
如果第一个选择器的日期是 12-12-2012,则输出是 11-12-2012
在我的代码中,我给了他们以下属性
lastPerPickerView.datePickerMode = UIDatePickerModeDate;