【问题标题】:Problem selecting date in UIDatePicker iphone在 UIDatePicker iphone 中选择日期的问题
【发布时间】:2011-08-12 15:28:59
【问题描述】:

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

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

谁能帮我解决这个问题???呵呵

谢谢你们!!

【问题讨论】:

  • 如果总是在时间之后 5 小时,为什么不直接减去 5 小时?
  • 你用什么代码来打印日期?根据您生成日期字符串的方式,您可能会获得 UTC 时间格式。

标签: iphone timezone uidatepicker hour


【解决方案1】:

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

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

NSString *stringToDisplay = [df stringFromDate:myDateObject];

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多