【问题标题】:swift check date is today or tomorrow快速检查日期是今天还是明天
【发布时间】:2021-11-28 21:50:26
【问题描述】:

我有一个整数数组,它是今天和明天日期的集合,我想根据日期的类型来分隔整数数组

let dateCollection = [
    1633722900,
    1633730500,
    1633754910,
    1633758913,
    1633820400,
    1633824000,
    1633827600,
    1633831200,
    1633834800,
    1633838400,
    1633842000
   ]

预期结果

let today: [Int] = [
    1633722900,
    1633730500,
    1633754910,
    1633758913
]

let tomorrow: [Int] = [  
    1633820400,
    1633824000,
    1633827600,
    1633831200,
    1633834800,
    1633838400,
    1633842000
]

我应该怎么做才能将它们分开,我已经做了一个扩展,将整数转换为日期,反之亦然,并将其显示为时间,我也已经创建了日期到时间的扩展

func getTimetringFromINT() -> String {
    let date = Date(timeIntervalSince1970: TimeInterval(self))
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "id")
    dateFormatter.dateFormat = "HH:mm"
    return dateFormatter.string(from: date)
}

selector i made, after convert the date to Time string

【问题讨论】:

    标签: arrays swift


    【解决方案1】:

    一种有效的方法是计算今天和明天之间午夜的 int 值,然后根据该值拆分数组

    let calendar = Calendar.current
    var today = [Int]()
    var tomorrow = [Int]()
    if let midnight = calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: .now))?.timeIntervalSince1970 {
        let limit = Int(midnight)
        dateCollection.forEach { $0 < limit ? today.append($0) : tomorrow.append($0) }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用Calendar.current.ordinality 比较各个日期的一年中的哪一天。下面是一个示例,它生成今天和明天的日期,然后将它们过滤回单独的数组:

      let today = Date()
      let todayInts = Array(0..<10).map { today.timeIntervalSince1970 + TimeInterval($0) }
      print("Today:", todayInts,"\n")
      
      let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)!
      let tomorrowInts = Array(0..<10).map { tomorrow.timeIntervalSince1970 + TimeInterval($0) }
      print("Tomorrow:", tomorrowInts,"\n")
      
      let allInts = todayInts + tomorrowInts
      
      let todayDayOfYear = Calendar.current.ordinality(of: .day, in: .year, for: today)!
      
      let filteredTodayInts = allInts.filter { Calendar.current.ordinality(of: .day, in: .year, for: Date(timeIntervalSince1970: $0)) == todayDayOfYear }
      print("Filtered today:", filteredTodayInts,"\n")
      
      let tomorrowDayOfYear = todayDayOfYear + 1
      
      let filteredTomorrowInts = allInts.filter { Calendar.current.ordinality(of: .day, in: .year, for: Date(timeIntervalSince1970: $0)) == tomorrowDayOfYear }
      print("Filtered tomorrow:", filteredTomorrowInts,"\n")
      
      

      【讨论】:

        【解决方案3】:

        另一种方法是使用Calendar API 和partition 数组获取第二天的开始日期(不管夏令时更改)

        let startOfTomorrow = Calendar.current.nextDate(after: Date(), 
                                               matching: DateComponents(hour: 0), 
                                               matchingPolicy: .nextTime)!
                                              .timeIntervalSince1970
        let splitIndex = dateCollection.partition{ $0 < Int(startOfTomorrow) }
        let tomorrowDates = dateCollection[..<splitIndex]
        let todayDates = dateCollection[splitIndex...]
        

        为了能够运行此声明 dateCollectionvar

        【讨论】:

          【解决方案4】:

          通过获取任意时间的 StartOfDay,我们可以对共享相同 StartOfDay 的所有 Date 进行分类。

          extension Date {
              var morning: Date { Calendar.current.startOfDay(for: self) }
              var nextMorning: Date { Calendar.current.date(byAdding: .day, value: 1, to: self)!.morning }
          }
          extension Int {
              var since1970: Date { Date(timeIntervalSince1970: TimeInterval(self)) }
          }
          

          在这种情况下:

          let todayMorning    = Date().morning     // Date().advanced(by: 3600*24 * -19).morning
          let tomorrowMorning = Date().nextMorning // Date().advanced(by: 3600*24 * -19).nextMorning
          
          let dateCollection  = [ ... ]
          
          let today    = dateCollection.filter{ $0.since1970.morning == todayMorning }
          let tomorrow = dateCollection.filter{ $0.since1970.morning == tomorrowMorning }
          

          【讨论】:

          • 86400 math 强烈建议不要使用,因为夏令时更改的日子可能有8280090000
          猜你喜欢
          • 2014-10-26
          • 2015-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-22
          • 2014-02-12
          • 2017-01-13
          • 2014-09-13
          相关资源
          最近更新 更多