簡體   English   中英

如何比較目標C中的兩個NSDate對象

[英]How to compare two NSDate objects in objective C

我有Date類型的對象。

我想比較它們,我用以下方式寫了一個if條件:

if (([startDate1 isEqualToDate:[self getDefaultDate]]) || (startDate1 != [ self getDefaultDate] && m_selectedDate >= m_currentDate1 && cycleStopped))
 {

///execute some condition

}

這種方法我是對還是錯?

還有一件事。 這樣做是對的:

if (dtdate > [m_array objectatIndex:i]
{

}

因為我得到隨機行為。

這是使用NSDate方法的示例, compare:

if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end
{
    // do something
}

從課程參考:

如果...

接收者和另一個日子完全相同,NSOrderedSame。

接收器的時間晚於anotherDate,NSOrderedDescending。

接收器的時間早於另一個日期,NSOrderedAscending。

例如,如果要比較兩個日期並找到它們之間的秒數,也可以使用timeIntervalSinceDate:方法。

編輯:

if(([startDate1 compare:[self getDefaultDate]] == NSOrderedSame) || ([startDate1 compare: [self getDefaultDate]] != NSOrderedSame && (([m_selectedDate compare: m_currentDate1] == NSOrderedDescending) || [m_selectedDate compare: m_currentDate1] == NSOrderedSame) && cycleStopped))

比較NSDates的簡便方法(可在http://webd.fr/637-comparer-deux-nsdate找到):

#import <Foundation/Foundation.h>

@interface NSDate (Compare)

-(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
-(BOOL) isLaterThan:(NSDate*)date;
-(BOOL) isEarlierThan:(NSDate*)date;
//- (BOOL)isEqualToDate:(NSDate *)date; already part of the NSDate API

@end

並實施:

#import "NSDate+Compare.h"

@implementation NSDate (Compare)

-(BOOL) isLaterThanOrEqualTo:(NSDate*)date {
    return !([self compare:date] == NSOrderedAscending);
}

-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date {
    return !([self compare:date] == NSOrderedDescending);
}
-(BOOL) isLaterThan:(NSDate*)date {
    return ([self compare:date] == NSOrderedDescending);

}
-(BOOL) isEarlierThan:(NSDate*)date {
    return ([self compare:date] == NSOrderedAscending);
}

@end

使用簡單:

if([aDateYouWant ToCompare isEarlierThanOrEqualTo:[NSDate date]]) // [NSDate date] is now
{
    // do your thing ...
}

請享用

我必須承認,我發現“比較”== NSWhatHaveYou業務太復雜而無法記住(即使它是正確的並且有效)。 NSDate還有另外兩種方法可以讓我的大腦更容易處理:earlyDate和laterDate。

考慮一下:

if ([myDate laterDate:today] == myDate) {
    NSLog(@"myDate is LATER than today");
} else {
    NSLog(@"myDate is EARLIER than today");
}

// likewise:
if ([myDate earlierDate:today] == myDate) {
    NSLog(@"myDate is EARLIER than today");
} else {
    NSLog(@"myDate is LATER than today");
}

這兩種方法都返回一個NSDate對象,該對象比另一個更早或更晚。

http://pinkstone.co.uk/how-to-compare-two-nsdates/

我真的很喜歡盧克的答案,它就像一個魅力。

我使用過這樣的東西:

If ([enteredDate compare: [NSDate Date]]== NSOrderedSame || NSOrderedDescending)
{
 //Do Something if the enteredDate is later or the same as today
}else
{
//if the enteredDate is before today's date
}

我不確定這是否有幫助,但只是重申盧克所寫的內容。

這是一種簡單的方法,可以檢查您認為的日期是否比您之前認為的日期更大(例如,將來)。 這就是它的作用。

  1. 獲取當前和未來日期的時間間隔(秒和frac秒)
  2. 如果測試日期更長(時間參考),則返回true

干得好

- (BOOL)isDateGreaterThanDate:(NSDate*)greaterDate and:(NSDate*)lesserDate{

    NSTimeInterval greaterDateInterval = [greaterDate timeIntervalSince1970];
    NSTimeInterval lesserDateInterval = [lesserDate timeIntervalSince1970];

    if (greaterDateInterval > lesserDateInterval) 
        return YES;

    return NO;
}

這也有效

BOOL *isGreaterDateTruelyGreater = [greaterDate timeIntervalSince1970] > [lesserDate timeIntervalSince1970];

objective-c處理日期的方式不太直觀。 基本上,您可以使用NSTimeInterval來獲取某個時間點的秒數。 它通常給出一個很長的數字,例如我正在寫這個1351026414.640865是從1970年開始的時間間隔。

時間點>時間點1351026415.640865> 1351026414.640865上面提前1秒,所以這是真的。

暫無
暫無

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

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