【问题标题】:Getting localized date range string from two NSDates从两个 NSDates 获取本地化的日期范围字符串
【发布时间】:2014-07-08 02:51:04
【问题描述】:

在获取两个 NSDates 并显示该范围的本地化字符串时遇到了问题。

例如,假设我的日期是 7 月 7 日和 7 月 9 日。我需要以下本地化字符串:

  • ZH:7 月 7 日至 9 日
  • 法国:7-9 朱丽叶
  • ES:7-9 胡里奥
  • DE:7. 之二。 9. 朱莉

显然在这里使用NSString stringWithFormat: 是行不通的。为简单起见,我们甚至不考虑您的范围是两个独立月的情况。我知道如何获取每个日期的格式化字符串,但它的格式设置在一个让我感兴趣的范围内。

有没有办法使用NSDateFormatter 来获得这个?我环顾四周越多,我就越觉得我需要为每个语言环境进行切换。

编辑:为了澄清,我只需要用户区域设置的日期范围。我不需要同时使用所有这些。

【问题讨论】:

  • @rishi 这不是这个问题的重复。
  • 不改变语言环境你想获取本地化日期?
  • @rishi 不仅仅是本地化日期。我需要知道显示日期范围的本地化格式。例如,如果我试图显示从今天(2014 年 7 月 8 日)到星期六(2014 年 7 月 12 日)的日期,我会在 EN 中得到“7 月 8 日至 12 日”。在 DE 我会得到“8. bis. 12. Juli”。由于每个语言环境对日期范围的表达方式不同,因此我不能像 [NSString stringWithFormat:@"%@ - %@", beginDate, endDate] 那样对其进行格式化。

标签: ios objective-c localization nsdate nsdateformatter


【解决方案1】:

经过进一步研究,看起来在 iOS 8.0+ 中您可以使用 NSDateIntervalFormatter 来执行此操作。现在这对我没有帮助,但很高兴知道它正在路上。

【讨论】:

    【解决方案2】:

    用一些示例代码详细说明亚当的答案:

    let formatter = NSDateIntervalFormatter()
    formatter.dateStyle = NSDateIntervalFormatterStyle.ShortStyle
    formatter.timeStyle = NSDateIntervalFormatterStyle.NoStyle
    let dateRangeStr = formatter.stringFromDate(startDate, toDate: endDate)
    

    【讨论】:

      【解决方案3】:

      如果您想删除年份信息,您需要在 NSDateIntervalFormatter 中显式设置 dateTemplate 参数。

      因此,只有使用此解决方案,您才能在 7 月 7 日至 9 日获得不带日期的结果。

      Objective-C

      NSDate *startDate = [NSDate new];
      NSDate *endDate = [[NSDate new] dateByAddingTimeInterval:10000];
      NSDateIntervalFormatter *df = [[NSDateIntervalFormatter alloc] init];
      df.dateTemplate = @"MMMd";
      
      NSString *detailedString = [df stringFromDate:startDate toDate:
          [trip.startDate dateByAddingNumberOfDays:endDate]];
      

      斯威夫特

      let df = DateIntervalFormatter()
      df.dateTemplate = "MMMd"
      let stringDate = df.string(from: Date(), to: Date().addingTimeInterval(10000))
      

      文档很简单:

      If the range smaller than the resolution specified by the dateTemplate, a single date format will be produced. If the range is larger than the format specified by the dateTemplate, a locale-specific fallback will be used to format the items missing from the pattern.
      
       For example, if the range is 2010-03-04 07:56 - 2010-03-04 19:56 (12 hours)
       - The pattern jm will produce
          for en_US, "7:56 AM - 7:56 PM"
          for en_GB, "7:56 - 19:56"
       - The pattern MMMd will produce
          for en_US, "Mar 4"
          for en_GB, "4 Mar"
       If the range is 2010-03-04 07:56 - 2010-03-08 16:11 (4 days, 8 hours, 15 minutes)
       - The pattern jm will produce
          for en_US, "3/4/2010 7:56 AM - 3/8/2010 4:11 PM"
          for en_GB, "4/3/2010 7:56 - 8/3/2010 16:11"
       - The pattern MMMd will produce
          for en_US, "Mar 4-8"
          for en_GB, "4-8 Mar"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 2015-02-11
        • 1970-01-01
        相关资源
        最近更新 更多