Comparaison NSDate en utilisant Swift

Je travaille sur une application qui nécessite de vérifier la date d’échéance pour les devoirs. Je veux savoir si une date d’échéance est dans la semaine suivante et si c’est le cas, effectuez une action.
La plupart de la documentation que je pourrais trouver se trouve dans Objective-C et je ne peux pas comprendre comment le faire dans Swift. Merci pour l’aide!!

    J’aime utiliser des extensions pour rendre le code plus lisible. Voici quelques extensions NSDate qui peuvent vous aider à nettoyer votre code et le rendre facile à comprendre. Je mets ça dans un fichier sharedCode.swift:

     extension NSDate { func isGreaterThanDate(dateToCompare: NSDate) -> Bool { //Declare Variables var isGreater = false //Compare Values if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending { isGreater = true } //Return Result return isGreater } func isLessThanDate(dateToCompare: NSDate) -> Bool { //Declare Variables var isLess = false //Compare Values if self.compare(dateToCompare) == NSComparisonResult.OrderedAscending { isLess = true } //Return Result return isLess } func equalToDate(dateToCompare: NSDate) -> Bool { //Declare Variables var isEqualTo = false //Compare Values if self.compare(dateToCompare) == NSComparisonResult.OrderedSame { isEqualTo = true } //Return Result return isEqualTo } func addDays(daysToAdd: Int) -> NSDate { let secondsInDays: NSTimeInterval = Double(daysToAdd) * 60 * 60 * 24 let dateWithDaysAdded: NSDate = self.dateByAddingTimeInterval(secondsInDays) //Return Result return dateWithDaysAdded } func addHours(hoursToAdd: Int) -> NSDate { let secondsInHours: NSTimeInterval = Double(hoursToAdd) * 60 * 60 let dateWithHoursAdded: NSDate = self.dateByAddingTimeInterval(secondsInHours) //Return Result return dateWithHoursAdded } } 

    Maintenant, si vous pouvez faire quelque chose comme ça:

     //Get Current Date/Time var currentDateTime = NSDate() //Get Reminder Date (which is Due date minus 7 days lets say) var reminderDate = dueDate.addDays(-7) //Check if reminderDate is Greater than Right now if(reminderDate.isGreaterThanDate(currentDateTime)) { //Do Something... } 

    Si vous voulez supporter == , < , > , <= ou >= pour NSDate s, il vous suffit de déclarer ceci quelque part:

     public func ==(lhs: NSDate, rhs: NSDate) -> Bool { return lhs === rhs || lhs.compare(rhs) == .OrderedSame } public func <(lhs: NSDate, rhs: NSDate) -> Bool { return lhs.compare(rhs) == .OrderedAscending } extension NSDate: Comparable { } 

    Voici comment vous comparez deux NSDates dans Swift, je l’ai juste testé dans le terrain de jeu de Xcode:

     if date1.compare(date2) == NSComparisonResult.OrderedDescending { NSLog("date1 after date2"); } else if date1.compare(date2) == NSComparisonResult.OrderedAscending { NSLog("date1 before date2"); } else { NSLog("dates are equal"); } 

    Donc, pour vérifier si une date d’ dueDate est dans une semaine à partir de maintenant:

     let dueDate=... let calendar = NSCalendar.currentCalendar() let comps = NSDateComponents() comps.day = 7 let date2 = calendar.dateByAddingComponents(comps, toDate: NSDate(), options: NSCalendarOptions.allZeros) if dueDate.compare(date2!) == NSComparisonResult.OrderedDescending { NSLog("not due within a week"); } else if dueDate.compare(date2!) == NSComparisonResult.OrderedAscending { NSLog("due within a week"); } else { NSLog("due in exactly a week (to the second, this will rarely happen in practice)"); } 

    Je l’ai toujours fait en une seule ligne:

     let greater = date1.timeIntervalSince1970 < date2.timeIntervalSince1970 

    Toujours lisible dans le bloc if

    Dans Swift3, la structure Date de la Foundation implémente désormais le protocole Comparable . Ainsi, les précédentes approches Swift2 NSDate sont remplacées par Swift3 Date .

     /** `Date` represents a single point in time. A `Date` is independent of a particular calendar or time zone. To represent a `Date` to a user, you must interpret it in the context of a `Calendar`. */ public struct Date : ReferenceConvertible, Comparable, Equatable { // .... more /** Returns the interval between the receiver and another given date. - Parameter another: The date with which to compare the receiver. - Returns: The interval between the receiver and the `another` parameter. If the receiver is earlier than `anotherDate`, the return value is negative. If `anotherDate` is `nil`, the results are undefined. - SeeAlso: `timeIntervalSince1970` - SeeAlso: `timeIntervalSinceNow` - SeeAlso: `timeIntervalSinceReferenceDate` */ public func timeIntervalSince(_ date: Date) -> TimeInterval // .... more /// Returns true if the two `Date` values represent the same point in time. public static func ==(lhs: Date, rhs: Date) -> Bool /// Returns true if the left hand `Date` is earlier in time than the right hand `Date`. public static func <(lhs: Date, rhs: Date) -> Bool /// Returns true if the left hand `Date` is later in time than the right hand `Date`. public static func >(lhs: Date, rhs: Date) -> Bool /// Returns a `Date` with a specified amount of time added to it. public static func +(lhs: Date, rhs: TimeInterval) -> Date /// Returns a `Date` with a specified amount of time subtracted from it. public static func -(lhs: Date, rhs: TimeInterval) -> Date // .... more } 

    Remarque …

    Dans Swift3, Date est struct , cela signifie qu’il s’agit d’un value type . NSDate est une class , c’est un reference type .

     // Swift3 let a = Date() let b = a //< `b` will copy `a`. // So, the addresses between `a` and `b` are different. // `Date` is some kind different with `NSDate`. 
     extension NSDate { // MARK: - Dates comparison func isGreaterThanDate(dateToCompare: NSDate) -> Bool { return self.compare(dateToCompare) == NSComparisonResult.OrderedDescending } func isLessThanDate(dateToCompare: NSDate) -> Bool { return self.compare(dateToCompare) == NSComparisonResult.OrderedAscending } func equalToDate(dateToCompare: NSDate) -> Bool { return self.compare(dateToCompare) == NSComparisonResult.OrderedSame } } 

    Si vous voulez comparer les dates avec la granularité (juste le même jour ou l’année, etc.) sur swift 3.

     func compareDate(date1:NSDate, date2:NSDate, toUnitGranularity: NSCalendar.Unit) -> Bool { let order = NSCalendar.current.compare(date1 as Date, to: date2 as Date, toGranularity: .day) switch order { case .orderedSame: return true default: return false } } 

    Pour les autres comparaisons de calendrier, modifiez .day à;

    .année .mois .day .heure .minute .second

    Swift implémente déjà la comparaison de date, utilisez simplement date1> date2 et ainsi de suite.

     /// Returns true if the two `Date` values represent the same point in time. public static func ==(lhs: Date, rhs: Date) -> Bool /// Returns true if the left hand `Date` is earlier in time than the right hand `Date`. public static func <(lhs: Date, rhs: Date) -> Bool /// Returns true if the left hand `Date` is later in time than the right hand `Date`. public static func >(lhs: Date, rhs: Date) -> Bool /// Returns a `Date` with a specified amount of time added to it. public static func +(lhs: Date, rhs: TimeInterval) -> Date /// Returns a `Date` with a specified amount of time subtracted from it. public static func -(lhs: Date, rhs: TimeInterval) -> Date /// Add a `TimeInterval` to a `Date`. /// /// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more. public static func +=(lhs: inout Date, rhs: TimeInterval) /// Subtract a `TimeInterval` from a `Date`. /// /// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more. public static func -=(lhs: inout Date, rhs: TimeInterval) 

    dans Swift 3, la date est comparable, donc nous pouvons directement comparer les dates comme

     let date1 = Date() let date2 = Date() let isGreater = date1 > date2 print(isGreater) let isEqual = date1 == date2 print(isEqual) 

    Ou bien

     let result = date1.compare(date2) switch result { case .OrderedAscending : print("date 1 is earlier than date 2") case .OrderedDescending : print("date 1 is later than date 2") case .OrderedSame : print("two dates are the same") } 

    meilleure façon de créer une extension à la date

     extension Date { fun isGreater(than date: Date) -> Bool { return self > date } func isSmaller(than date: Date) -> Bool { return self < date } func isEqual(to date: Date) -> Bool { return self == date } } 

    usage let isGreater = date1.isGreater(than: date2)

    Cette fonction a fonctionné pour moi pour comparer si une date (startDate) était après la date de fin où les deux étaient définies comme variables NSDate:

     if startDate.compare(endDate as Date) == ComparisonResult.orderedDescending 

    mise en œuvre dans Swift

     let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSSsortingng let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(documentsPath, error: nil) let filesAndProperties = NSMutableArray() for file in files! { let filePath = documentsPath.ssortingngByAppendingSsortingng(file as NSSsortingng) let properties = NSFileManager.defaultManager().atsortingbutesOfItemAtPath(filePath, error: nil) let modDate = properties![NSFileModificationDate] as NSDate filesAndProperties.addObject(NSDictionary(objectsAndKeys: file, "path", modDate, "lastModDate")) } let sortedFiles = filesAndProperties.sortedArrayUsingComparator({ (path1, path2) -> NSComparisonResult in var comp = (path1.objectForKey("lastModDate") as NSDate).compare(path2.objectForKey("lastModDate") as NSDate) if comp == .OrderedDescending { comp = .OrderedAscending } else if comp == .OrderedAscending { comp = .OrderedDescending } return comp }) 
     var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" let dateData: Ssortingng = dateFormatter.ssortingngFromDate(date1) let testDate: Ssortingng = dateFormatter.ssortingngFromDate(date2) print(dateData == testDate) 

    Nous avons un scénario pour vérifier l’heure actuelle se trouve à deux resockets (deux dates). Par exemple, je veux vérifier le délai actuel entre l’ouverture de la clinique (hôpital) et l’heure de fermeture.

    Utilisez le code simple.

      NSDate * now = [NSDate date]; NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; [outputFormatter setDateFormat:@"HH:mm:ss"]; //current time NSSsortingng *currentTimeSsortingng = [outputFormatter ssortingngFromDate:now]; NSDate *dateCurrent = [outputFormatter dateFromSsortingng:currentTimeSsortingng]; NSSsortingng *timeStart = @"09:00:00"; NSSsortingng *timeEnd = @"22:00:00"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm:ss"]; NSDate *dateStart= [formatter timeStart]; NSDate *dateEnd = [formatter timeEnd]; NSComparisonResult result = [dateCurrent compare:dateStart]; NSComparisonResult resultSecond = [date2 compare:dateEnd]; if(result == NSOrderedDescending && resultSecond == NSOrderedDescending) { NSLog(@"current time lies in starting and end time"); }else { NSLog(@"current time doesn't lie in starting and end time"); } 

    Pour swift 3, vous pouvez utiliser la fonction ci-dessous pour comparer deux dates.

     func compareDate(dateInitial:Date, dateFinal:Date) -> Bool { let order = Calendar.current.compare(dateInitial, to: dateFinal, toGranularity: .day) switch order { case .orderedSame: return true default: return false } } 

    toGranularity peut être modifié en fonction des contraintes sur lesquelles vous souhaitez appliquer votre comparaison.

    S’étendre sur SashaZ

    Swift iOS 8 et supérieur Lorsque vous avez besoin de plus que des comparaisons de dates plus ou moins importantes. Par exemple est-ce le même jour ou le jour précédent, …

    Note: N’oubliez jamais le fuseau horaire. Le fuseau horaire du calendrier a une valeur par défaut, mais si vous n’aimez pas la valeur par défaut, vous devez définir le fuseau horaire vous-même. Pour savoir quel jour il est, vous devez savoir dans quel fuseau horaire vous demandez.

     extension Date { func compareTo(date: Date, toGranularity: Calendar.Component ) -> ComparisonResult { var cal = Calendar.current cal.timeZone = TimeZone(identifier: "Europe/Paris")! return cal.compare(self, to: date, toGranularity: toGranularity) } } 

    Utilisez-le comme ceci:

     if thisDate.compareTo(date: Date(), toGranularity: .day) == .orderedDescending { // thisDate is a previous day } 

    D’un exemple plus complexe. Recherchez et filtrez toutes les dates d’un tableau, provenant du même jour que “findThisDay”:

     let formatter = DateFormatter() formatter.timeZone = TimeZone(identifier: "Europe/Paris") formatter.dateFormat = "yyyy/MM/dd HH:mm:ss" let findThisDay = formatter.date(from: "2018/11/05 08:11:08")! _ = [ formatter.date(from: "2018/12/05 08:08:08")!, formatter.date(from: "2018/11/05 08:11:08")!, formatter.date(from: "2018/11/05 11:08:22")!, formatter.date(from: "2018/11/05 22:08:22")!, formatter.date(from: "2018/11/05 08:08:22")!, formatter.date(from: "2018/11/07 08:08:22")!, ] .filter{ findThisDay.compareTo(date: $0 , toGranularity: .day) == .orderedSame } .map { print(formatter.ssortingng(from: $0)) } 
     someArray.sort({($0.dateAdded?.timeIntervalSinceReferenceDate)! < ($1.dateAdded?.timeIntervalSinceReferenceDate)!}) 

    dateAdded est une variable NSDate dans mon object

     class MyClass { let dateAdded: NSDate? }