如何比较大于当前日期的日期数组

How to compare array of dates with greater than current date

如何比较 'date' 字符串数组与大于和小于 swift 中的当前 date。 这是我从服务器获取的数组。

"leave_dates": [
        "03-09-2017",
        "04-09-2017",
        "05-09-2017",
        "04-09-2017",
        "01-09-2017",
        "07-09-2017",
        "09-09-2017",
        "10-09-2017",
        "12-09-2017",
        "13-09-2017",
        "15-09-2017",
        "16-09-2017",
        "23-09-2017"
    ],

首先,您需要将 string date 转换为 Date 对象,您可以使用 DateFormatter 来完成此操作。然后你可以使用 filter 检查日期是否大于今天:

let dates =  ["03-09-2017", "04-09-2017", "05-09-2017", "04-09-2017", "01-09-2017", "07-09-2017", "09-09-2017", "10-09-2017", "12-09-2017", "13-09-2017", "15-09-2017", "16-09-2017", "23-09-2017"]

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"

let greaterThanToday = dates.filter({ dateFormatter.date(from: [=10=])! > Date() })
print(greaterThanToday) // ["23-09-2017"]

使用Dateformtterclass将其转换为日期,然后与今天的日期进行比较。您可以将代码包装在 CustomDateFormatter 中并使用静态函数来比较日期和 return bool.

class CustomDateFormatter {

    // MARK: - Properties
    private static var dateFormatter: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd-MM-yyyy"
        return formatter
    }

    // MARK: - Public
    static func date(from string: String) -> Date? {
        return dateFormatter.date(from: string)
    }

    static func campare(_ string: String, with date: Date = Date()) -> Bool {
        guard let newDate = dateFormatter.date(from: string) else {
            return false
        }
        return newDate > date
    }
}

现在你可以像

一样使用它了
    let leave_dates = ["03-09-2017", "04-09-2017", "05-09-2017", "04-09-2017", "01-09-2017", "07-09-2017", "09-09-2017", "10-09-2017", "12-09-2017", "13-09-2017", "15-09-2017", "16-09-2017", "23-09-2017"]
    let today = Date()
    let greaterThanToday = leave_dates.filter { (date) -> Bool in
        return CustomDateFormatter.campare(date, with: today)
    }
    print(greaterThanToday)

i 使用堆栈溢出建议解决了我的问题。我发布了我的答案,它可能对其他人有帮助。

extension Date {
    var startOfWeek: Date? {
        let gregorian = Calendar(identifier: .gregorian)
        guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
        return gregorian.date(byAdding: .day, value: 1, to: sunday)
    }

    var endOfWeek: Date? {
        let gregorian = Calendar(identifier: .gregorian)
        guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
        return gregorian.date(byAdding: .day, value: 7, to: sunday)
    }
    func generateDatesArrayBetweenTwoDates(startDate: Date , endDate:Date) ->[Date]
    {
        var datesArray: [Date] =  [Date]()
        var startDate = startDate
        let calendar = Calendar.current
        let fmt = DateFormatter()
        fmt.dateFormat = "dd-MM-YYYY"
        while startDate <= endDate {
            datesArray.append(startDate)
            startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
        }
        return datesArray
    }
}

我正在像这样使用该功能。

func dateList() {
        let startWeek = Date().startOfWeek
        let endWeek = Date().endOfWeek
        datesBetweenArray = Date().generateDatesArrayBetweenTwoDates(startDate: startWeek! , endDate: endWeek!) as NSArray
        print(datesBetweenArray)
}