Comparing NSDates without time component

后端 未结 17 1593
轮回少年
轮回少年 2020-11-27 12:49

In a swift playground, I have been using

NSDate.date() 

But, this always appears with the time element appended. For my app I need to ignor

相关标签:
17条回答
  • 2020-11-27 13:12

    There are several useful methods in NSCalendar in iOS 8.0+:

    startOfDayForDate, isDateInToday, isDateInYesterday, isDateInTomorrow
    

    And even to compare days:

    func isDate(date1: NSDate!, inSameDayAsDate date2: NSDate!) -> Bool
    

    To ignore the time element you can use this:

    var toDay = Calendar.current.startOfDay(for: Date())
    

    But, if you have to support also iOS 7, you can always write an extension

    extension NSCalendar {
        func myStartOfDayForDate(date: NSDate!) -> NSDate!
        {
            let systemVersion:NSString = UIDevice.currentDevice().systemVersion
            if systemVersion.floatValue >= 8.0 {
                return self.startOfDayForDate(date)
            } else {
                return self.dateFromComponents(self.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: date))
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:13

    Use this Calendar function to compare dates in iOS 8.0+

    func compare(_ date1: Date, to date2: Date, toGranularity component: Calendar.Component) -> ComparisonResult
    


    passing .day as the unit

    Use this function as follows:

    let now = Date()
    // "Sep 23, 2015, 10:26 AM"
    let olderDate = Date(timeIntervalSinceNow: -10000)
    // "Sep 23, 2015, 7:40 AM"
    
    var order = Calendar.current.compare(now, to: olderDate, toGranularity: .hour)
    
    switch order {
    case .orderedDescending:
        print("DESCENDING")
    case .orderedAscending:
        print("ASCENDING")
    case .orderedSame:
        print("SAME")
    }
    
    // Compare to hour: DESCENDING
    
    var order = Calendar.current.compare(now, to: olderDate, toGranularity: .day)
    
    
    switch order {
    case .orderedDescending:
        print("DESCENDING")
    case .orderedAscending:
        print("ASCENDING")
    case .orderedSame:
        print("SAME")
    }
    
    // Compare to day: SAME
    
    0 讨论(0)
  • 2020-11-27 13:14

    When you NSDate.date() in the playground, you see the default description printed. Use NSDateFormatter to print a localized description of the date object, possibly with only the date portion.

    To zero out specific portions of a date (for the sake of comparison), use NSDateComponents in conjunction with NSCalendar.

    0 讨论(0)
  • 2020-11-27 13:16

    In Swift 4:

    func compareDate(date1:Date, date2:Date) -> Bool {
        let order = NSCalendar.current.compare(date1, to: date2, toGranularity: .day)
        switch order {
        case .orderedSame:
            return true
        default:
            return false
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:18

    Swift:

    extension NSDate {
    
        /**
        Compares current date with the given one down to the seconds.
        If date==nil, then always return false
    
        :param: date date to compare or nil
    
        :returns: true if the dates has equal years, months, days, hours, minutes and seconds.
        */
        func sameDate(date: NSDate?) -> Bool {
            if let d = date {
                let calendar = NSCalendar.currentCalendar()
                if NSComparisonResult.OrderedSame == calendar.compareDate(self, toDate: d, toUnitGranularity: NSCalendarUnit.SecondCalendarUnit) {
                    return true
                }
    
            }
            return false
        }
    }
    
    0 讨论(0)
  • 2020-11-27 13:19

    Two Dates comparisions in swift.

        // Date comparision to compare current date and end date.
        var dateComparisionResult:NSComparisonResult = currentDate.compare(endDate)
    
        if dateComparisionResult == NSComparisonResult.OrderedAscending
        {
            // Current date is smaller than end date.
        }
        else if dateComparisionResult == NSComparisonResult.OrderedDescending
        {
            // Current date is greater than end date.
        }
        else if dateComparisionResult == NSComparisonResult.OrderedSame
        {
            // Current date and end date are same.
        }
    
    0 讨论(0)
提交回复
热议问题