【发布时间】:2012-03-20 04:43:09
【问题描述】:
好的,我已经为此工作了几天,但被卡住了。我想做的是将当前时间与预定时间进行比较。我想要发生的是,每天在某些时间我希望某些代码触发,但不是在我指定的时间之前。
所以,基本上如果“当前时间”与“预定时间”相同或等于“预定时间”,则触发此代码。或者触发这行代码。
听起来很简单,但我无法比较这两次。
我已经将一个字符串格式化为这样的日期
NSString* dateString = @"11:05 PM";
NSDateFormatter* firstDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[firstDateFormatter setDateFormat:@"h:mm a"];
[firstDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate* date = [firstDateFormatter dateFromString:dateString];
NSLog(@"date %@",date);
然后我像这样抓住当前时间
NSDate *currentTime = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]];
NSDateComponents* comps = [[NSCalendar currentCalendar]
components: NSHourCalendarUnit |NSMinuteCalendarUnit
|NSSecondCalendarUnit fromDate:currentTime];
[[NSCalendar currentCalendar] dateFromComponents:comps];
那么我该如何比较这两次呢?我已经尝试了我能想到的一切,请帮助。
【问题讨论】: