2013-12-10 26 views
0

你好,我見過很多NSDate的比較在這個網站 但他們都似乎是很複雜的,因爲我只需要知道 如果日期現在已經過去4點或之前,下午4點的NSDate過去下午4點

也許有一些簡單的方法來實現這一目標?

[鏈接](Check if the time and date is between a particular date and time

,但它似乎很漫長而複雜的我只需要簡單的布爾回答過去下午4時或不

- (BOOL)checkTime 
{ 

    NSDate* now = [NSDate date]; 
    NSDate *endDate; 
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    endDate = [formatter dateFromString:@"2012-12-07 16:00:00"];//well here I have a problem how do I set end day to today 4pm ? 

    NSLog(@"%d",[now compare:endDate]); 
    if([now compare:endDate] < 0) 
     return YES; 
    else if([now compare:endDate] > 0) 
     return NO; 
    else 
     return NO; 

} 

編輯:答案後,我想出了這個代碼

- (BOOL)checkTime 
{ 
    NSDate *date = [NSDate date]; 
    NSDateFormatter *df = [NSDateFormatter new]; 
    [df setDateFormat:@"HH"]; 
    int intS =9; 
    NSInteger HourStart = intS; 
    NSInteger hour = [[df stringFromDate:date] integerValue]; 
    int hourE = 16; 
    NSInteger HourEnd = hourE; 
    if((hour >HourStart) && (hour < HourEnd)) 
     return YES; 
    else 
     return NO; 
} 

現在它工作得很好,但我不知道它會在另一個日曆工作組等

如果你只想檢查4,然後用 hh,如果16然後使用 HH

+2

顯示您的代碼,並在5分鐘內我會幫你。 –

+0

+1給你Anoop先生。 – Piyush

+0

是的,對不起,這裏是我的代碼,我需要設置endDate到今天下午4點 –

回答

2

下面會給你時間:

NSDate *now = [NSDate date]; 

NSDateFormatter *df = [NSDateFormatter new]; 
[df setDateFormat:@"hh"]; 

NSInteger hour = [[df stringFromDate:now] integerValue]; 

注意。

或簡單的話說就是HH爲24小時格式。

+0

HH應該是24小時格式嗎? –

+0

然後我應該比較兩個整數? 我會試一試謝謝! –

+0

@ user2957713:更清晰地更新了答案 –

4

一般來說,我傾向於使用NSDateComponentsNSCalendar進行這些日曆計算,因爲您不知道用戶正在使用的日曆。

下面是做在一個類別比較上NSDate使用過期組件的方法:

#import <Foundation/Foundation.h> 

@interface NSDate (Foo) 
- (BOOL)isAfterFourPM; 
@end 

@implementation NSDate (Foo) 

- (BOOL)isAfterFourPM { 
    unsigned int flags = NSHourCalendarUnit; 
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:flags fromDate:self]; 
    return (comps.hour >= 16); 
} 

@end 

int main(int argc, char *argv[]) { 
    @autoreleasepool { 
     NSDate *now = [NSDate date]; 
     NSLog(@"is after 4 PM? - %@",([now isAfterFourPM][email protected]"YES":@"NO")); 

     // let's try with a different time (17h) 
     unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; 
     flags |= NSHourCalendarUnit; 
     NSDateComponents *currentComps = [[NSCalendar currentCalendar] components:flags fromDate:now]; 
     currentComps.hour = 17; 
     NSDate *afterFourDate = [[NSCalendar currentCalendar] dateFromComponents:currentComps]; 

     NSLog(@"is after 4 PM? - %@",([afterFourDate isAfterFourPM][email protected]"YES":@"NO")); 
    } 
} 

此打印:

2013-12-10 05:16:05.921 Untitled 2[43453:507] is after 4 PM? - NO 
2013-12-10 05:16:05.921 Untitled 2[43453:507] is after 4 PM? - YES 

(在〜5點中部時間)

在這種情況下,只需使用當前日曆從日期獲得NSHourCalendarUnit組件,並比較hour屬性y on NSDateComponents to 16.

+0

我不明白* _ *,您發佈的代碼不好? 還是不錯? –

+0

適合我。我所做的一點是,你不需要製作一個「今天下午4點」的日期,僅僅是爲了做出「下午4點以後」的比較。您可以使用日期組件API進行比較。見上面的方法'isAfter4PM'。 – FluffulousChimp

+0

噢,我用上面的答案 - 「NSInteger hour = [[df stringFromDate:now] integerValue];」 所以現在我有一個整數持有的小時,然後我把它與另一個整數設置爲16, 我不知道在目前它現在的作品,但你認爲它可能有問題其他時間格式? –