【问题标题】:Remove only time from array of dates Swift仅从日期数组中删除时间 Swift
【发布时间】:2019-07-12 11:22:00
【问题描述】:

我从这样的 api 获取日期:

["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]

但我想要的是从这个日期数组中删除时间以获得这样的日期

我想要这样的结果

["2019-07-12", "2019-07-09", "2019-07-09", "2019-07-05", "2019-07-04", "2019-07-02", "2019-07-01"]

我想要的原因是因为我需要在FSCalendar 上显示事件。我怎样才能做到这一点?

【问题讨论】:

  • 都是字符串吗?他们有固定的格式吗?你能只取前10个字符吗? – 你肯定尝试过一些东西,不要犹豫,展示你的尝试!
  • 使用日期格式化程序进行转换
  • @MartinR ....是的,它们都是字符串,所有日期的格式都相同,我尝试了这个答案stackoverflow.com/questions/35392538/…,但显示了致命错误
  • 如果都是字符串使用split strings并将第一个对象存储到数组中
  • @Anbu.Karthik... 我看到了这个答案,但他们都只是拆分字符串变量而不是数组

标签: ios swift


【解决方案1】:

您可以简单地使用 compactMap(_:)components(separatedBy:) 的组合,例如,

let arr = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]

let result = arr.compactMap({ $0.components(separatedBy: " ").first })

【讨论】:

    【解决方案2】:

    请检查以下代码:

        let arr = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]
    
        var arrOnlyDates = [String]()
        for (_,str) in arr.enumerated(){
            let s = str.components(separatedBy: " ")
            arrOnlyDates.append(s[0])
        }
        print(arrOnlyDates)
    

    【讨论】:

    • 如果只丢弃偏移量,为什么还要使用enumerated()
    【解决方案3】:

    您可以像这样简单地获取日期数组:

    let dateArray = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]
    
    let onlyDatesArray = dateArray.compactMap({ $0.components(separatedBy: " ")[0] })
    
    print("only dates -> \(onlyDatesArray)")
    

    【讨论】:

      【解决方案4】:
      let dateStrings = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]    
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
      let dates = dateStrings.compactMap { dateFormatter.date(from: $0) }
      dateFormatter.dateFormat = "yyyy-MM-dd"
      let formatedDateStrings = dates.compactMap { dateFormatter.string(from: $0) }
      

      【讨论】:

        【解决方案5】:

        由于日期格式不会改变,只需从每个字符串中删除前 10 个字符

        let dates = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]
        let datesWithoutTime = dates.map{ String($0.prefix(10)) }
        

        【讨论】:

          猜你喜欢
          • 2015-10-13
          • 1970-01-01
          • 2012-11-23
          • 1970-01-01
          • 2018-08-20
          • 2012-12-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多