iOS中的日期比较

时间:2013-08-21 03:41:48

标签: ios objective-c cocoa-touch

我正在尝试检查两个日期之间的时间是否超过3个月,但没有成功。这是我正在尝试的。我做错了什么吗?即使我在昨天设置了detailTextDate,它仍然会显示evaluateLabel。我需要设置dateFormat吗?

    NSString *detailTextDate = @"August 11, 2013";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterLongStyle;

    NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *todaysDate = [NSDate date];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSMonthCalendarUnit
                                                        fromDate:startDate
                                                          toDate:todaysDate
                                                         options:0];
    if(components.month >= 3){
        evaluationLabel.hidden = NO;
    }

}

//tried using NSYearCalendarUnit, Month, Day etc.

//startDate = 2013-08-11 06:00:00 +0000, detailTextDate = August 11, 2013, components = //<NSDateComponents: 0x176c3c70>
//    Month: 0

6 个答案:

答案 0 :(得分:0)

你只要求NSYearCalendarUnit;你可能想要NSMonthCalendarUnit

答案 1 :(得分:0)

以下日志12[component month][component day]为9(请注意,我更改了开始日期,因为在您的示例中,月差异为零):

  NSString *detailTextDate = @"August 11, 2012";

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  dateFormatter.dateStyle = NSDateFormatterLongStyle;

  NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
  NSDate *todaysDate = [NSDate date];

  NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                   initWithCalendarIdentifier:NSGregorianCalendar];

  NSUInteger flags = NSMonthCalendarUnit | NSDayCalendarUnit;
  NSDateComponents *components = [gregorianCalendar components:flags
                                                      fromDate:startDate
                                                        toDate:todaysDate
                                                       options:0];

  NSLog(@"%ld", (long)[components month]);
  NSLog(@"%ld", (long)[components day]);

答案 2 :(得分:0)

我解决了我的问题。不知道为什么但这有效。希望我将来能帮助别人。

    NSString *detailTextDate = @"May 23, 2013";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    dateFormatter.dateStyle = NSDateFormatterLongStyle;



    NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *todaysDate = [NSDate date];

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

        NSLog(@"startDate = %@, detailTextDate = %@, components = %ld", startDate, detailTextDate, (long)components.day);
    if(components.day >= 90){
        evaluationLabel.hidden = NO;
    }

}

答案 3 :(得分:0)

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"MMMM dd, yyyy"];
    NSDate *date1 = [dateFormatter dateFromString:detailTextDate];

    NSDate *todaysDate = [NSDate date];

    unsigned int unitFlags = NSDayCalendarUnit,NSMonthCalendarUnit,NSYearCalendarUnit;

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:todaysDate  options:0];

    int month = [comps month];
    NSLog(@"%d",month);

    if(month>0)
    {

    }

答案 4 :(得分:-1)

您可以使用timeIntervalSinceNow来确定过去发生的事情。

这会返回NSTimeIntervaltypedef代表double代表秒数。所以你可以做这样的事情:

NSTimeInterval month = 60.0 * 60.0 * 24.0 * 30.0;
NSTimeInterval timeUntilNow = -[startDate timeIntervalSinceNow];
if (timeUntilNow > month * 3.0) {
   ...
}

答案 5 :(得分:-1)

 NSString *detailTextDate = @"August 11, 2013";

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.dateStyle = NSDateFormatterLongStyle;

NSDate *enddate = [dateFormatter dateFromString:detailTextDate];
NSDate* currentdate = [NSDate date];
NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
NSInteger secondsBetweenDates = distanceBetweenDates / 60;

if(secondsBetweenDates>0){
// Here you can compare seconds
}