-3

我有一个objective-c 示例如何将天差计算为midnites 的数量,发表在Apple 的“日期和时间编程指南”中。是否可以在 Swift 中做同样的事情?

    @implementation NSCalendar (MySpecialCalculations)
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
     NSInteger startDay=[self ordinalityOfUnit:NSDayCalendarUnit
          inUnit: NSEraCalendarUnit forDate:startDate];
     NSInteger endDay=[self ordinalityOfUnit:NSDayCalendarUnit
          inUnit: NSEraCalendarUnit forDate:endDate];
     return endDay-startDay;
}
@end
4

2 回答 2

0

我会做这样的事情:

extension NSCalendar {
    func daysWithinEraFromDate(startDate: NSDate, toDate endDate: NSDate) -> NSInteger {
        let startDay = self.ordinalityOfUnit(.DayCalendarUnit, inUnit: .EraCalendarUnit, forDate: startDate)
        let endDay = self.ordinalityOfUnit(.DayCalendarUnit, inUnit: .EraCalendarUnit, forDate: endDate)
        return endDay - startDay
    }

}
于 2015-04-04T15:15:16.427 回答
0

基于基本的 Objective-C 到 Swift 转换文档:

extension NSCalendar {
  public func daysApart (sd: NSDate, ed: NSDate) -> Int {
    return ordinalityOfUnit(.DayCalendarUnit, inUnit: .EraCalendarUnit, forDate: ed) -
           ordinalityOfUnit(.DayCalendarUnit, inUnit: .EraCalendarUnit, forDate: sd)
  }
}
于 2015-04-04T15:13:22.987 回答