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

发布于 2024-11-29 13:28:04 字数 375 浏览 0 评论 0原文

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

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

谁能帮我解决这个问题吗??? XD

谢谢你们!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-12-06 13:28:05

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

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];  
}
客…行舟 2024-12-06 13:28:04

如果您还没有使用它,我建议您执行以下操作:

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

NSString *stringToDisplay = [df stringFromDate:myDateObject];

NSDateFormatter 应该为您处理任何时区问题。您可以在此处阅读更多内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文