从日期范围创建日期的字符串数组 [英] Create string array of dates from Dates range
问题描述
我具有Date()
属性. startingAt
和endingAt
.还有一个Date()数组,它们是alreadyRegistred
.我必须创建一个日期在startingAt
和endingAt
之间的字符串数组.包括StartingAt
和endingAt
,最后一个要求是排除alreadyRegistred
日期.
I have Date()
properties. startingAt
and endingAt
. And an array of Date(), which are alreadyRegistred
. I have to create an array of strings with dates between startingAt
and endingAt
. StartingAt
and endingAt
are included and the last requirement is to exclude alreadyRegistred
dates.
您有一些优雅的主意,怎么做?感谢您的帮助!
Do you have some elegant idea, how to do it? Thanks for help!
最终数组中的最大日期数约为7天.
Maximum number of dates in final array will be about 7 days.
推荐答案
尝试一下,看看:
// 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]
Resulting Array - ["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"]
这篇关于从日期范围创建日期的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!