比较NSDate + Period

时间:2014-02-15 07:05:13

标签: iphone objective-c nsdate

我有开始日期和持续时间(期间)。例如startDate = '2014-02-12' period = 2。我希望日期2014-02-122014-02-142014-02-16,....我需要确定当前日期是否已标记。

3 个答案:

答案 0 :(得分:0)

您可以使用

获得所需的日期

- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds

seconds = period*3600*24

答案 1 :(得分:0)

检查开始日期和当前日期之间的差异是否为偶数 天数,请使用NSDateComponents

NSDate *startDate = ...;
NSDate *currentDate = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *diff = [cal components:NSDayCalendarUnit fromDate:startDate
                                  toDate:currentDate options:0];
NSInteger days = diff.day;
if (days % 2 == 0) {
    // even number of days between start date and current date
}

答案 2 :(得分:0)

计算从startDate到givenData的天数,检查结果是否可以被句点整除。

+ (NSInteger)daysWithinEraFromDate:(NSDate *)startDate toDate:(NSDate *)endDate {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setTimeZone:[NSTimeZone localTimeZone]];
    // for timezone issue
    NSDate *newDate1 = [startDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
    NSDate *newDate2 = [endDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
    NSInteger startDay = [gregorian ordinalityOfUnit:NSDayCalendarUnit
                                            inUnit: NSEraCalendarUnit forDate:newDate1];
    NSInteger endDay = [gregorian ordinalityOfUnit:NSDayCalendarUnit
                                          inUnit: NSEraCalendarUnit forDate:newDate2];
    return endDay - startDay;
}