0

I have 2 date pickers, each one deference from the ether (280 day) the pickers change correctly, but mydatepicker.date when NSlog it or put it inUITextfield the date become date - 1 day or date + 1 day

here is my code :

the first picker:

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);

the second one:

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);

if the date of 1st picker is 12-12-2012 the out put is 11-12-2012

in my code i gave them the properity of

  lastPerPickerView.datePickerMode = UIDatePickerModeDate;
Mutawe
  • 6,464
  • 3
  • 47
  • 90

2 Answers2

1

The output you are getting is correct logically. But if you want the output according to your requirement I think you should change value 280 to 279.

1

Try adding this lines to your code,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:00"]];

or

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
Dhruv
  • 2,153
  • 3
  • 21
  • 45