【问题标题】:NSDateFormatter not giving proper response while setting dateNSDateFormatter 在设置日期时没有给出正确的响应
【发布时间】:2015-05-14 05:14:08
【问题描述】:

我有一点问题。 请看这里,理解这个问题。

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale systemLocale]];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"];
    self.datePicker.userInteractionEnabled=YES;
    NSString *dateString=[dateFormatter stringFromDate:datePicker.date];

这是我要返回日期的块。但是当我在那里设置NSLocale 时,这不会给我正确的回应。

我想要dd-MMM-yyyy (24-Mar-2008) 格式的DATE。但它给出了回应:

iOS8.0 及更高版本 --> 24-M03-2004

iOS7.0 及更早版本 --> 24-Month03-2004

由于某些依赖性,我无法从上面的块中删除 [dateFormatter setLocale:[NSLocale systemLocale]];。 请建议我如何处理这个问题,

【问题讨论】:

  • [dateFormat setTimeZone:[NSTimeZone localTimeZone]];而不是 [dateFormatter setLocale:[NSLocale systemLocale]];你会很高兴的
  • 你能分享你的 datepicker.date 值吗?
  • @adnan 你的答案是正确的。
  • @muku datepicker.date 值来自 NSDatePicker。
  • 这是由于 [dateFormatter setLocale:[NSLocale systemLocale]] 和它唯一受支持的新版本 iOS 所致。希望你能因为这个信息而得到一些逻辑......

标签: ios objective-c iphone nsdate nsdateformatter


【解决方案1】:

尝试使用

[dateFormatter setLocale:[NSLocale currentLocale]];

代替

[dateFormatter setLocale:[NSLocale systemLocale]];

这将返回您想要的格式。

【讨论】:

    【解决方案2】:

    这将解决您的问题

    [dateFormatter setLocale:[NSLocale currentLocale]];
    

    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    

    【讨论】:

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