7

我正在尝试比较两天的 NSDate 对象,我第一次使用 NSDateComponents 来计算我的两个日期对象之间的差异,但它工作正常。

    NSString *dateStr =@"2012-11-05 15:00:00";
    NSDate *date = [dateFormat dateFromString:dateStr];
    NSDateComponents *datesDiff = [calendar components: NSDayCalendarUnit
                                              fromDate: date
                                                toDate: [NSDate date]
                                               options: 0];

这并不能解决我的问题。实际上,当 date1 = 2012-11-05 23:00:00 和 date2 = 2012-11-06 10:00:00 时,两个日期之间的差是 0 天和 11 小时。我正在寻找可以让我检测到日期变化的东西,换句话说,当 date1 = 2012-11-05 23:00:00 和 date2 = 2012-11-06 00:01:00 时,会有一天的差异。

如果有人知道解决方案,我会欢迎他的建议。提前致谢

4

4 回答 4

19

我认为您正在寻找这个(来自日期和时间编程指南):

清单 13 两个日期之间的天数,作为两个日期之间的午夜数

@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

编辑:斯威夫特版本:

extension NSCalendar {
    func daysWithinEraFromDate(startDate: NSDate, endDate: NSDate) -> Int {
        let startDay = self.ordinalityOfUnit(.Day, inUnit: NSCalendarUnit.Era, forDate: startDate)
        let endDay = self.ordinalityOfUnit(.Day, inUnit: NSCalendarUnit.Era, forDate: endDate)
        return endDay - startDay
    }
}
于 2012-11-05T18:18:47.963 回答
6
NSString *start = @"2010-09-01";
NSString *end = @"2010-12-01";

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
[f release];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                    fromDate:startDate
                                                      toDate:endDate
                                                     options:0];
[gregorianCalendar release];

组件现在拥有差异。

NSLog(@"%ld", [components day]);
于 2013-07-04T07:55:30.727 回答
0
func daysBetweenDates(startDate: NSDate, endDate: NSDate) -> Int
{
    let calendar = NSCalendar.currentCalendar()

    let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])

    return components.day
}

或者

let calendar = NSCalendar.currentCalendar();
let component1 = calendar.component(.Day, fromDate: fromDate)
let component2 = calendar.component(.Day, fromDate: toDate)
let difference  = component1 - component2
于 2016-07-01T06:39:58.790 回答
0

对于斯威夫特 3:

从@masa的回答扩展:

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
    }
}
于 2017-03-09T15:38:41.663 回答