在UIDatePicker iphone中选择日期时出现问题

时间:2011-08-12 15:29:00

标签: iphone timezone uidatepicker hour

我在表单中使用UIDatePicker,但问题是当我选择日期和时间时,文本字段中的时间是在选择器中显示的时间后5小时。我已经读过日期选择器中有一个错误,但我不知道如何解决这个问题。我需要展示墨西哥的时间。我试过这样做,但没有任何改变。

  

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

任何人都可以帮我这个??? XD

谢谢你们!!

2 个答案:

答案 0 :(得分:3)

如果您还没有使用它,我建议您这样做:

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

NSString *stringToDisplay = [df stringFromDate:myDateObject];

NSDateFormatter应该为您处理任何时区问题。您可以阅读更多here.

答案 1 :(得分:0)

o:这将有助于你

//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];  
}