2011-05-04 60 views

回答

3
- (BOOL)past5pm 
{ 
    NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
    NSDateComponents *components = [gregorianCalender components:NSHourCalendarUnit fromDate:[NSDate date]]; 

    if([components hour] >= 17) // NSDateComponents uses the 24 hours format in which 17 is 5pm 
     return YES; 

    return NO; 
} 
0

試試這個

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
     [dateFormatter setDateFormat:@"hh:mm:ss a"]; 

     NSDate* date = [NSDate date]; 

     //Get the string date 
     NSString* str = [dateFormatter stringFromDate:date]; 

     NSDate *firstDate = [dateFormatter dateFromString:@"05:00:00 PM"]; 

     NSDate *secondDate = [dateFormatter dateFromString:str]; 

     NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate]; 

     NSLog(@"Time Diff - %f",timeDifference); 
1
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setDateFormat:@"HH.mm"]; 
NSDate *currentDate = [NSDate date]; 
NSString *curDate = [dateFormatter stringFromDate:currentDate]; 

if ([curDate doubleValue] >= 17.00) 
{ 
    //set your bool 
} 
+1

比較ObjC對象和double看起來有點不對,你不這麼認爲嗎? – JustSid 2011-05-04 08:48:05

+1

@JustSid Thankz buddy ...我做了編輯...現在它應該工作 – Aaron 2011-05-04 08:56:54

0

你或許可以使用普通的C風格的代碼,並獲得差額作爲一個整數,然後決定你需要從比較函數根據返回的內容羯羊的差異是積極的或消極的。你也可以用這種方式比較分鐘。不要忘記導入time.h.

time_t now = time(NULL); 
    struct tm oldCTime; 
    localtime_r(&now, &oldCTime); 
    int hours = oldCTime.tm_hour; 
    int diff = 17-hours; 
    NSLog(@"Time difference is: %d.", diff); 
相關問題