繁体   English   中英

获取Swift中两个日期之间所有星期二的日期

[英]Get the dates of all tuesdays between two dates in 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并创建一个自定义方法来返回与日期组件匹配的start日期和end日期之间的所有日期:

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 +0000, 2020-10-06 03:00:00 +0000, 2020-10-13 03:00:00: +0000, 2020-10-20 03:00:00 +0000, 2020-10-27 03:00:00 +0000, 2020-11-03 03:00:00 +0000, 2020-11-10 0 :00 +0000, 2020-11-17 03:00:00 +0000, 2020-11-24 03:00:00 +0000, 2020-12-01 03:00:00 +0000, 2020-03-12 :00:00 +0000, 2020-12-15 03:00:00 +0000, 2020-12-22 03:00:00 +0000, 2020-12-29 03:00:00 +0000, 2021-0 05 03:00:00 +0000, 2021-01-12 03:00:00 +0000, 2021-01-19 03:00:00 +0000, 2021-01-26 03:00:00 +0020-2 02-02 03:00:00 +0000, 2021-02-09 03:00:00 +0000]

在很大程度上取决于你必须从什么开始。 你有DateString的值吗?

我更喜欢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

在我的测试中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM