【问题标题】:How to show date picker in US Format(DD/MM/YY)?如何以美国格式(DD/MM/YY)显示日期选择器?
【发布时间】:2017-03-09 07:30:59
【问题描述】:
  if (_providerDatePicker) _providerDatePicker.hidden =
     !_providerDatePicker.hidden; if (_providerToolbar)
     _providerToolbar.hidden = !_providerToolbar.hidden; float screenWidth = [UIScreen mainScreen].bounds.size.width;
     _providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 244, self. view.bounds.size.width,
     44)];

      UIBarButtonItem *done = [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self
     action:@selector(dismissActionSheet:)];
     _providerToolbar.items = @[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
     target:nil action:nil], done];
     _providerToolbar.barStyle = UIBarStyleBlackOpaque; [self.view addSubview:_providerToolbar];

     _providerDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 200,
     screenWidth, 200.0f)];
     _providerDatePicker.backgroundColor = [UIColor whiteColor];
     _providerDatePicker.datePickerMode = UIDatePickerModeDate;    // [_providerDatePicker setLocale:[[NSLocale alloc]
     initWithLocaleIdentifier:@"US"]];

     _providerDatePicker.date = [NSDate date];

我正在使用按钮操作中的方法来显示 DatePicker,但它正在运行,但它显示如图所示。我想将其更改为 DD/MM/YY。帮帮我?

【问题讨论】:

标签: ios objective-c


【解决方案1】:

问题是您不想以美国格式实际显示它。美国的日期格式是月/日/年。

如果你想要英文和日/月/年,你可以使用英国语言​​环境:

_providerDatePicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];

一般来说,篡改语言环境来设置特定格式并不是一个好主意。您应该使用系统格式,因为这是用户想要使用的格式。

【讨论】:

    【解决方案2】:

    您可以在这里查看答案: iPhone:DatePicker dd/mm/yyyy

    NSLocale *uk = [[NSLocale alloc] initWithLocaleIdentifier:@"US"];
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setLocale:uk];
    [myDatePicker setCalendar:cal];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多