尝试使用此代码进行日期之间的任何比较...您不应该以字符串的形式比较日期。比较转换为字符串之前的日期。dateFromString
通过将确切的日期格式指定为dateString 的格式,使用格式化程序的功能将 self.serverDate 转换为日期格式。然后使用以下函数比较日期。
-(void) callAfterSixtySecond:(NSTimer*) t
{
NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"HH:mm"];
NSDate *todayTime = [formatter dateFromString:[formatter stringFromDate:today]];
NSDate *requiredTime = [formatter dateFromString:[formatter stringFromDate:newDate]];
NSComparisonResult result;
result = [todayTime compare:requiredTime ]; // comparing two dates
if(result == NSOrderedAscending)
NSLog(@"today is less");
else if(result == NSOrderedDescending)
NSLog(@"newDate is less");
else if(result == NSOrderedSame)
NSLog(@"Both dates are same"); // time has reached. Update Label using setText method of label
else
NSLog(@"Date cannot be compared");
}
您将需要使用 NSTimer 每分钟运行一次此方法...
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];