1

我已经阅读了关于这个主题的所有问题和答案以及所有教程,但由于某种原因,它对我不起作用。总是告诉我这两个日期是同一个日期!

请有人帮我弄清楚,我只想检查一个是否比另一个大(包括日期和时间 - 不包括秒)或者它们是否相等。

这是我的代码:

- (BOOL)isEndDateIsBiggerThanCurrectDate:(NSDate *)checkEndDate
{
    NSString *endd = [NSDateFormatter localizedStringFromDate:checkEndDate
                                                      dateStyle:NSDateFormatterShortStyle
                                                      timeStyle:NSDateFormatterShortStyle];

    NSString *curreeeent = [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                      dateStyle:NSDateFormatterShortStyle
                                                      timeStyle:NSDateFormatterShortStyle];

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

    NSDate * newCurrent = [df dateFromString:endd];
    NSDate * newEnd = [df dateFromString:curreeeent];

    switch ([newCurrent compare:newEnd])
    {
        case NSOrderedAscending:
            return YES;
            break;
        case NSOrderedSame:
            return NO;
            break;
        case NSOrderedDescending:
            return NO;
            break;
    }
}

非常感谢!

4

4 回答 4

1

为此,您必须使用NSCalender

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit);


NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:[NSDate date]];
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate: checkEndDate];

NSDate *first = [calendar dateFromComponents:firstComponents];
NSDate *second = [calendar dateFromComponents:secondComponents];

NSComparisonResult result = [first compare:second];
if (result == NSOrderedAscending) {
    //checkEndDate is before now
} else if (result == NSOrderedDescending) {
    //checkEndDate is after now
}  else {
    //both are same
}
于 2012-10-23T10:05:09.460 回答
0

您真的应该使用时间间隔,而不是在日期和字符串之间进行转换。

以下内容应该适合您的需求:

//current time
NSDate *now = [NSDate date];

//time in the future
NSDate *distantFuture = [NSDate distantFuture];

//gather time interval
if([now timeIntervalSinceDate:distantFuture] > 0)
{
    //huzzah!
}
于 2012-10-23T09:30:59.913 回答
0

我得到了答案,只需检查两个日期之间的确切时间并进行比较。

- (BOOL)isEndDateIsSmallerThanCurrent:(NSDate *)checkEndDate
{
    NSDate* enddate = checkEndDate;
    NSDate* currentdate = [NSDate date];
    NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
    double secondsInMinute = 60;
    NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;

    if (secondsBetweenDates == 0)
        return YES;
    else if (secondsBetweenDates < 0)
        return YES;
    else
        return NO;
}
于 2012-10-23T09:54:36.087 回答
-2

为什么不将日期更改为自 1970 年以来的时间间隔并按此排序。非常简单的数字比较,比字符串比较快得多,而且它们总是排序正确,不像 1,10,11,2,21,22,3,....

NSDate *now = [NSDate date];
NSTimeInterval ti = [now timeIntervalSince1970];

而已。没有新的对象创建,速度更快,对 CPU 的负担也更少。

看这里你如何摆脱秒,但这很容易,因为你有数字,秒。请参阅此处如何将 NSDate 的秒数设置为零

于 2012-10-23T09:28:43.487 回答