我有Date()
个属性。 startingAt
和endingAt
。还有一个Date()数组,它是alreadyRegistred
。我必须创建一个日期在startingAt
和endingAt
之间的字符串数组。包含StartingAt
和endingAt
,最后一项要求是排除alreadyRegistred
个日期。
你有一些优雅的想法,怎么做?谢谢你的帮助!
编辑:最终数组中的最大日期数约为7天。
答案 0 :(得分:1)
不要忘记Date
基本上只是一个时间戳,您可以访问addingTimeInterval(_:)方法。
知道这一点,很容易在两个日期之间进行一些计算。
我并不完全了解您所需的业务逻辑,但这是一个在两个日期之间生成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)")
答案 1 :(得分:1)
试试这个,看看:
// 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/201“,”09/01/2018“,”10/01/2018“,”11/01/2018“,”12/01/2018“,”13/01 / 2018年“,”2014年1月14日“,”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" ]
答案 2 :(得分:0)
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
}