2013-04-06 94 views
1

下面是我的兩個日期選擇器的代碼,我需要使用if條件或開關將它們與當前日期進行比較。將uipickerdate與當前日期進行比較

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
dateFormatter.timeZone=[NSTimeZone defaultTimeZone]; 
dateFormatter.timeStyle=NSDateFormatterShortStyle; 
dateFormatter.dateStyle=NSDateFormatterShortStyle; 
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date]; 
NSLog(@"Start time is %@",dateTimeString); 


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init]; 
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone]; 
dateFormatter2.timeStyle=NSDateFormatterShortStyle; 
dateFormatter2.dateStyle=NSDateFormatterShortStyle; 
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date]; 
NSLog(@"End time is %@",dateTimeString2); 

在我的.h

@property (weak, nonatomic) IBOutlet UIDatePicker *startTime; 
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime; 

我該怎麼辦呢?我是否需要將它們存儲到NSDate或NSTime中,因爲我只需要時間?

另外,Objective C中有If範圍嗎?

感謝,

+1

我在代碼中看不到任何日期選擇器。你在說什麼日期選擇器? – matt 2013-04-06 02:27:36

+0

謝謝@matt檢查我的編輯並注意我的.h,starttime.date和endtime.date實際上是我的日期選擇器 – user1949873 2013-04-06 02:52:43

+0

那麼,我的答案仍然存在。 – matt 2013-04-06 03:04:34

回答

2

只需添加這

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; 

添加函數以迎合通知當它被張貼

-(void)applicationEnteredBackground 
{ 
    //do all the above steps here when you want. 
} 

而且在通知

-(void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
+0

優秀的直接答案!你知道我該如何將它鏈接到'AppDelegate'的'DidEnterBackground'方法? – user1949873 2013-04-06 04:16:20

+0

@ user1949873意味着你想要什麼?你想使用任何值嗎? – iSpark 2013-04-06 10:19:01

+1

好吧,讓我直接得到這個,當應用程序進入後臺狀態時,你想要做這一切嗎?好吧,看看我的編輯。 – satheeshwaran 2013-04-06 12:16:02

2

的唯一日期,我在你的代碼中看到的是startTime.dateendTime.date。我不知道你在哪裏得到這些,但他們大概是NSDates。另一方面,當前日期是[NSDate date]。因此,您可以使用NSDate的compare:方法或任何其他幾種比較方法進行比較(請參閱NSDate文檔)。

+0

你能給我舉個例子嗎? – user1949873 2013-04-06 03:09:01

+0

'NSComparisonResult res = [[NSDate date] compare:startTime。日期]' – matt 2013-04-06 03:10:29

2

NSDate執行isEqualToDate:它測試兩個日期是否相同直到微秒。它還實現了earlierDate:laterDate:,您可以使用它來構建範圍檢查。

你也可以建立一些助手,讓你使用timeIntervalSinceDate:來控制平等檢查的靈敏度。沒有日期範圍對象,但您也可以在支票之間添加一張支票...

@interfce NSDate (Comparison) 

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance; 
- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance; 

@end 

@implementation NSDate (Comparison) 

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance { 

    NSTimeInterval difference = [self timeIntervalSinceDate:aDate]; 
    return fabs(difference) < tolerance; 
} 

- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance { 

    NSTimeInterval startDifference = [self timeIntervalSinceDate:start]; 
    if (startDifference < tolerance) return NO; 

    NSTimeInterval endDifference = [end timeIntervalSinceDate:self]; 
    if (endDifference < tolerance) return NO; 

    return YES; 
} 

@end 

我還沒有測試過這些。如果你想之間兩個日期使用此方法

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate 


if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0) 
{ 
    //start time greater than today 
} 

else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0) 
{ 
    //start time less than today 
} 

else 
{ 
    //both dates are equal 
} 

爲了知道什麼時候該應用已經進入後臺使用的通知在viewDidLoad中添加此語句來計算區間

if ([[NSDate date] isEqualToDate:startTime.date]) { 
NSLog(@"currentDate is equal to startTime"); 
} 

if ([[NSDate date] isEqualToDate:endTime.date]) { 
NSLog(@"currentDate is equal to endTime"); 
} 

+0

NSDate有比較方法,所以爲什麼重新發明那個輪子? (不要批評,我想我可能會錯過一些東西) – matt 2013-04-06 03:04:12

+0

這是一個有效的問題。我添加了測試,以包含在我認爲OP需要的範圍內,以及降低測試靈敏度的想法。 – danh 2013-04-06 04:44:27

2

你也可以像下面使用的NSDate的比較類中刪除觀察者的dealloc功能:方法,

NSDateFormatter* df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"MM-dd-yyyy"]; 
NSDate* date1 = [df dateFromString:@"12-23-2046"]; 

NSDate *date2 = [NSDate date]; 

if ([date1 compare:date2] == NSOrderedSame){ 
    NSLog(@"Same"); 
} 
if ([date2 compare:date1]==NSOrderedAscending) { 
    NSLog(@"AScending"); 
} 
if ([date2 compare:date1]== NSOrderedDescending) { 
    NSLog(@"Descending"); 

} 

起飛你的startTime,endTime而不是date1,希望它能幫助你。

相關問題