3

I´m using the UIDatePicker in a form but the problem is that when I select the date and time, the time in the text field is 5 hours after the time showed in the picker. I've read that there's a bug in date picker but I don't know how to solve this. I need to show the time of Mexico. I've tried doing this but nothing change.

datePicker.calendar = [NSCalendar autoupdatingCurrentCalendar];
datePicker.timeZone = [NSTimeZone localTimeZone];
datePicker.locale = [NSLocale currentLocale];

Can anyone help me with this??? XD

Thank you guys!!

Rafael Jimeno
  • 626
  • 2
  • 8
  • 20
  • If it's always 5 hours after the time, why don't you just subtract off the 5 hours? – msgambel Aug 12 '11 at 16:31
  • What code do you use to print the date? You may be getting the UTC time format depending on how you're generating the date string. – Ben Mosher Aug 12 '11 at 17:11

2 Answers2

3

If you aren't using it already, I'd suggest doing:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterMediumStyle];

NSString *stringToDisplay = [df stringFromDate:myDateObject];

NSDateFormatter should take care of any time zone issues for you. You can read more here.

Ben Mosher
  • 13,251
  • 7
  • 69
  • 80
0

o this: this will help you

//Date Picker
(void)textFieldDidBeginEditing:(UITextField *)aTextField {    
  [aTextField resignFirstResponder];  

  pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];  

  UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];  
  pickerView.datePickerMode = UIDatePickerModeDate;  
  pickerView.hidden = NO;  
  pickerView.date = [NSDate date];  

  UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
  pickerToolbar.barStyle = UIBarStyleBlackOpaque;  
  [pickerToolbar sizeToFit];  

  NSMutableArray *barItems = [[NSMutableArray alloc] init];  

  UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
  [barItems addObject:flexSpace];  

  UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
  [barItems addObject:doneBtn];  

  UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
  [barItems addObject:cancelBtn];  

  [pickerToolbar setItems:barItems animated:YES];  

  [pickerViewPopup addSubview:pickerToolbar];  
  [pickerViewPopup addSubview:pickerView];  
  [pickerViewPopup showInView:self.view];  
  [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];    
}  

(void)doneButtonPressed:(id)sender{  
//Do something here here with the value selected using [pickerView date] to get that value  
    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
}  

(void)cancelButtonPressed:(id)sender{  
  [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
}
Manuel
  • 3,828
  • 6
  • 33
  • 48