2013-01-23 56 views
-4

可能重複:
How to Check if an NSDate occurs between two other NSDates我如何檢查當前時間是在開始時間和結束時間之間?

在我的應用我有在這樣的字符串時,

開始時間(串):10:00 AM

endtime(string):12:22 PM

我的當前時間是上午11點05分

我該如何檢查當前時間是在開始時間和結束時間之間?

+0

這可能是最簡單的。 http://stackoverflow.com/questions/9807370/how-to-check-if-date-from-date-picker-is-between-two-other-dates-objective-c – mlstudent

回答

0

用這樣的方式:

首先使用NSDateFormatter您的字符串轉換爲NSDate

然後直接比較這兩個日期與currentDate總是[NSDate date]

您可以使用NSDate的這兩個實例方法。

isGreaterThan:isLessThan:

或者,

if (! (([currentDate compare:startDate]==NSOrderedAscending) && ([currentDate compare:endDate]==NSOrderedDescending)){ 
    NSLog(@"Valid date"); 
} 
-1

首先嚐試刪除AM和PM

像它的PM然後在那段時間內添加+12。

然後檢查天氣在「:」之前的當前時間的數字是大於開始時間還是小於結束時間..如果它返回true,那麼您可以在「:」之後檢查數字。

相關問題