在Swift中获取两次约会之间的所有星期二的日期



我需要的是获取两个日期之间特定日期的所有日期。

例如。

日期:星期二开始日期:2020年9月21日结束日期:2021年2月15日

输出:中每周二的日期

我是Date运营的新手,所以我不太知道如何才能做到这一点。

有人知道吗?感谢

您可以在while循环中使用日历方法nextDate(after:, matching:, matchingPolicy, repeatedTimePolicy:, direction:),并添加一个条件来检查结果日期是否小于或等于结束日期:

func nextDate(after date: Date, matching components: DateComponents, matchingPolicy: MatchingPolicy, repeatedTimePolicy: RepeatedTimePolicy = .first, direction: SearchDirection = .forward) -> Date?

var start = DateComponents(calendar: .current, year: 2020, month: 9, day: 21).date!
let end = DateComponents(calendar: .current, year: 2021, month: 2, day: 15).date!
var dates: [Date] = []
while let date = Calendar.current.nextDate(after: start, matching: DateComponents(weekday: 3), matchingPolicy: .strict), date <= end {
dates.append(date)
start = date
}
print(dates) 

您还可以扩展DateInterval并创建一个自定义方法来返回startend日期之间与日期组件匹配的所有日期:

extension DateInterval {
func dates(matching components: DateComponents) -> [Date] {
var start = self.start
var dates: [Date] = []
while let date = Calendar.current.nextDate(after: start, matching: components, matchingPolicy: .strict), date <= end {
dates.append(date)
start = date
}
return dates
}
}

用法:

let start = DateComponents(calendar: .current, year: 2020, month: 9, day: 21).date!
let end = DateComponents(calendar: .current, year: 2021, month: 2, day: 15).date!
let dateInterval: DateInterval = .init(start: start, end: end)
let tuesdays = dateInterval.dates(matching: .init(weekday: 3))
print(tuesdays) 

这将打印

【2020-09-22 03:00:00+0000,2020-09-29 03:00:00+000,2020-10-06 03:00:00+0000,2020-10-13 03:00:00%+0000,2020 0-20 03:00-00+0000、2020-10-27 03:0:00:00+0000、2020-11-03 03:00:00:00+00002020-11-10 03:00:00/00002020-11-17 03:00:00.00+00002020-101-24 03:00:00-00+00002020-12-01 03:00:000+00002020-208 03:00:00:00002020-12-15 03:00:00\00002020-12-22 03:00+00000:00+00002020-12-29 03:00:00+00002021-01-05 03:00:00+00002021-01-12 03:00:00\00002021-01-19 03:00:00+0002021-01-26 03:00:000+0002021-02-02 03:0:00:00+0000 2021-02-09 03:00:00:00+0000]

这在很大程度上取决于您必须从什么开始。您的值是Date还是String

我更喜欢Date,但为了测试,我从String开始。。。

let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
guard let startDate = formatter.date(from: "2020/09/21"), let endDate = formatter.date(from: "2021/02/15") else { return [] }

当然,您的输入可能采用不同的格式,因此您需要对此进行补偿。

接下来,我们需要找到从开始日期起的下周二

var calendar = Calendar.current
calendar.firstWeekday = 2 // Make Monday the starting point, don't know if I need to compensate for this, but not taking any risks
var components = DateComponents()
components.weekday = 3 // Look for Tuesdays
guard var date = calendar.nextDate(after: startDate, matching: components, matchingPolicy: .nextTime) else { return [] }

接下来,我们只想将date增加7天,这样我们就可以跳到下一个Tuesday

var datesOfInterest: [Date] = []
while date < endDate {
datesOfInterest.append(date)
guard date = calendar.date(byAdding: .day, value: 7, to: date) else { 
// You might want to consider this to be an error, but I'm
// just going for brevity
return [] 
}
}
return datesOfInterest

显然,这假设您有一个返回Date值数组的函数。

只是为了测试。。。

let test = DateFormatter()
test.dateFormat = "eeee dd MM yyyy"
print(datesOfInterest.map { test.string(from: $0) }.joined(separator: ", "))

输出。。。

Tuesday 22 09 2020, Tuesday 29 09 2020, Tuesday 06 10 2020, Tuesday 13 10 2020, Tuesday 20 10 2020, Tuesday 27 10 2020, Tuesday 03 11 2020, Tuesday 10 11 2020, Tuesday 17 11 2020, Tuesday 24 11 2020, Tuesday 01 12 2020, Tuesday 08 12 2020, Tuesday 15 12 2020, Tuesday 22 12 2020, Tuesday 29 12 2020, Tuesday 05 01 2021, Tuesday 12 01 2021, Tuesday 19 01 2021, Tuesday 26 01 2021, Tuesday 02 02 2021, Tuesday 09 02 2021

在我的测试中

最新更新