Number of days between two NSDate objects

前端 未结 4 1419
悲&欢浪女
悲&欢浪女 2020-12-16 04:31

I\'m trying to compare two days of NSDate objects, I used at the first time NSDateComponents to calculate difference between my two dates Objects, but it is working correct

4条回答
  •  轮回少年
    2020-12-16 05:05

    For Swift 3:

    Expanding from @masa's answer:

    extension Calendar {
        func daysWithinEraFromDate(startDate: Date, endDate: Date) -> Int? {
            guard let startDay = ordinality(of: .day, in: .era, for: startDate) else {
                return nil
            }
    
            guard let endDay = ordinality(of: .day, in: .era, for: endDate) else {
                return nil
            }
    
            return endDay - startDay
        }
    }
    

提交回复
热议问题