2011-05-04 57 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); 
相关问题