簡體   English   中英

NSDate比較無法正常工作

[英]NSDate compare is not working well

我已經閱讀了有關該主題的所有問題和答案以及所有教程,但是由於某種原因,它不適用於我。 總是向我顯示兩個日期是同一日期!

請有人幫我弄清楚,我只想檢查一個是否大於另一個(包括日期和時間-不包括秒),或者是否相等。

這是我的代碼:

- (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;
    }
}

非常感謝你!

為此,您必須使用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
}

您實際上應該使用時間間隔,而不是在日期和字符串之間進行轉換。

類似以下內容應適合您的需求:

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

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

//gather time interval
if([now timeIntervalSinceDate:distantFuture] > 0)
{
    //huzzah!
}

我得到了答案,只需檢查兩個日期之間的確切時間並進行比較即可。

- (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;
}

為什么不將日期更改為自1970年以來的時間間隔,並按此排序。 數字比較極其簡單,比字符串比較要快得多,而且它們始終會正確排序,而不像1,10,11,2,21,22,3,....

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

而已。 沒有新的對象創建,更快,更少的CPU負擔。

在這里查看如何擺脫秒數,但是這很容易,因為您有秒數。 請參閱此處如何將NSDate的秒設置為零

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM