从日期范围创建日期字符串数组

我有Date()属性。 startingAtendingAt 。 还有一个Date()数组,它已经被alreadyRegistred 。 我必须创建一个字符串数组,其日期介于startingAtendingAt之间。 包括StartingAtendingAt ,最后一个要求是排除alreadyRegistred日期。

你有一些优雅的想法,怎么做? 感谢帮助!

编辑:最终数组中的最大日期数约为7天。

不要忘记Date基本上只是一个时间戳,并且您可以访问addsTimeInterval(_ :)方法。

知道这一点,很容易在两个日期之间进行一些计算。

我不完全了解您所需的业务逻辑,但这是一个天真的实现,可以在两个Dates之间生成Dates 。 我相信你可以在操场上跑步并探索一下。

 import UIKit func intervalDates(from startingDate:Date, to endDate:Date, with interval:TimeInterval) -> [Date] { guard interval > 0 else { return [] } var dates:[Date] = [] var currentDate = startingDate while currentDate <= endDate { currentDate = currentDate.addingTimeInterval(interval) dates.append(currentDate) } return dates } let startingDate = Date() // now let endDate = Date(timeIntervalSinceNow: 3600 * 24 * 7) // one week from now let intervalBetweenDates:TimeInterval = 3600 * 3// three hours let dates:[Date] = intervalDates(from: startingDate, to: endDate, with: intervalBetweenDates) let dateFormatter = DateFormatter() dateFormatter.dateStyle = .long dateFormatter.timeStyle = .long let dateStrings = dates.map{dateFormatter.string(from: $0)} print("NOW : \(startingDate)") for (index, string) in dateStrings.enumerated() { print("\(index) : \(string)") } print("END DATE : \(endDate)") 

试试看,看看:

 // Start & End date string let startingAt = "01/01/2018" let endingAt = "08/03/2018" // Sample date formatter let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd/MM/yyyy" // start and end date object from string dates var startDate = dateFormatter.date(from: startingAt) ?? Date() let endDate = dateFormatter.date(from: endingAt) ?? Date() // String date array, to be excluded let alreadyRegistred = ["01/01/2018", "15/01/2018", "10/02/2018", "20/02/2018", "05/03/2018"] // Actual operational logic var dateRange: [String] = [] while startDate <= endDate { let stringDate = dateFormatter.string(from: startDate) startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date() if (alreadyRegistred.contains(stringDate)) { continue } else { dateRange.append(stringDate) } } print("Resulting Array - \(dateRange)") 

结果如下:

结果数组 - [“02/01/2018”,“03/01/2018”,“04/01/2018”,“05/01/2018”,“06/01/2018”,“07/01/2018 “,”08/01/2018“,”09/01/2018“,”10/01/2018“,”11/01/2018“,”12/01/2018“,”13/01/2018“, “14/01/2018”,“16/01/2018”,“17/01/2018”,“18/01/2018”,“19/01/2018”,“20/01/2018”,“21 / 01/2018“,”22/01/2018“,”23/01/2018“,”24/01/2018“,”25/01/2018“,”26/01/2018“,”27/01 / 2018“,”28/01/2018“,”29/01/2018“,”30/01/2018“,”31/01/2018“,”01/02/2018“,”02/02/2018 “,”03/02/2018“,”04/02/2018“,”05/02/2018“,”06/02/2018“,”07/02/2018“,”08/02/2018“, “09/02/2018”,“11/02/2018”,“12/02/2018”,“13/02/2018”,“14/02/2018”,“15/02/2018”,“16 / 02/2018“,”17/02/2018“,”18/02/2018“,”19/02/2018“,”21/02/2018“,”22/02/2018“,”23/02 / 2018“,”24/02/2018“,”25/02/2018“,”26/02/2018“,”27/02/2018“,”28/02/2018“,”01/03/2018 “,”02/03/2018“,”03/03/2018“,”04/03/2018“,”06/03/2018“,”07/03/2018“,”08/03/2018“]

在此处输入图像描述

 let startDate = Date() let endDate = Date().addingTimeInterval(24*60*60*10) // i did this to get the end date for now var stringdateArray = [String]() if let days = getNumberofDays(date1: startDate, date2: endDate) { for i in 0...days-1 { let date = startDate.addingTimeInterval(Double(i)*24*3600) let stringDate = getStringDate(fromDate: date, havingFormat: "yyyy-MM-dd") if !(alreadyRegisteredArray.contains(stringDate)) { // checking if already registered stringdateArray.append(stringDate) } } } 

和我们的助手方法

 let dateFormatter = DateFormatter() func getStringDate(fromDate: Date,havingFormat: String) -> String { dateFormatter.dateFormat = havingFormat dateFormatter.amSymbol = "AM" dateFormatter.pmSymbol = "PM" let date = dateFormatter.string(from: fromDate) return date } func getNumberofDays(date1: Date, date2: Date) -> Int? { let calendar = NSCalendar.current let date1 = calendar.startOfDay(for: date1) let date2 = calendar.startOfDay(for: date2) let components = calendar.dateComponents([.day], from: date1, to: date2) return components.day }