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

时间:2017-09-22 06:01:36

标签: ios arrays swift string nsdate

如何在swift中将'date'字符串数组与大于和小于当前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"
    ],

3 个答案:

答案 0 :(得分:1)

首先,您需要将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: $0)! > Date() })
print(greaterThanToday) // ["23-09-2017"]

答案 1 :(得分:0)

使用Dateformtter类将其转换为日期,然后与今天的日期进行比较。您可以将代码包装在CustomDateFormatter中,并使用静态函数来比较日期并返回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)

答案 2 :(得分:0)

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

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)
}