比较ios中的两个时间值?

在我的应用程序中,我想检查当前时间是保存在variables中的时间之前还是之后。

像我的时间1是time1=@"08:15:12"; 而我的time2是time2=@"18:12:8";

所以我想比较time1和time2.Current这些variables是在NSString格式。

以下是用于获取时间的代码,我不知道如何比较它们。

码:

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"HH:MM:SS"; [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"ewetwet%@",[dateFormatter stringFromDate:now]); 

请帮帮我

使用以下代码将nsstring转换为nsdate并进行比较。

 NSString *time1 = @"08:15:12"; NSString *time2 = @"18:12:08"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm:ss"]; NSDate *date1= [formatter dateFromString:time1]; NSDate *date2 = [formatter dateFromString:time2]; NSComparisonResult result = [date1 compare:date2]; if(result == NSOrderedDescending) { NSLog(@"date1 is later than date2"); } else if(result == NSOrderedAscending) { NSLog(@"date2 is later than date1"); } else { NSLog(@"date1 is equal to date2"); } 
 // Use compare method. NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *startDate = [formatter dateFromString:@"2012-12-07 7:17:58"]; NSDate *endDate = [formatter dateFromString:@"2012-12-07 7:17:59"]; if ([startDate compare: endDate] == NSOrderedDescending) { NSLog(@"startDate is later than endDate"); } else if ([startDate compare:endDate] == NSOrderedAscending) { NSLog(@"startDate is earlier than endDate"); } else { NSLog(@"dates are the same"); } // Way 2 NSTimeInterval timeDifference = [endDate timeIntervalSinceDate:startDate]; double minutes = timeDifference / 60; double hours = minutes / 60; double seconds = timeDifference; double days = minutes / 1440; NSLog(@" days = %.0f,hours = %.2f, minutes = %.0f,seconds = %.0f", days, hours, minutes, seconds); if (seconds >= 1) NSLog(@"End Date is grater"); else NSLog(@"Start Date is grater"); 

首先,您需要将时间string转换为NSDate => 将时间string转换为date格式iOS 。

然后使用下面的代码

 NSDate *timer1 =... NSDate *timer2 =... NSComparisonResult result = [timer1 compare:timer2]; if(result == NSOrderedDescending) { // time 1 is greater then time 2 } else if(result == NSOrderedAscending) { // time 2 is greater then time 1 } else { //time 1 is equal to time 2 } 

你可以使用这个链接

根据NSDate的Apple文档比较:

 Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date. - (NSComparisonResult)compare:(NSDate *)anotherDate Parameters anotherDate The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X. Return Value If: The receiver and anotherDate are exactly equal to each other, NSOrderedSame The receiver is later in time than anotherDate, NSOrderedDescending The receiver is earlier in time than anotherDate, NSOrderedAscending 

换一种说法:

 if ([date1 compare:date2]==NSOrderedSame) ... 

请注意,读取和写入可能会更容易:

 if ([date2 isEqualToDate:date2]) ... 

请参阅Apple文档了解这一点。 如果你觉得有什么问题,你也可以在这里使用这个链接。 在这里,你可以通过尼尔森布赖恩的答案。

我在本文结束时已经这样做了 –

 NSDate * currentDateObj=@"firstDate"; NSDate * shiftDateFieldObj=@"SecondDate"; NSTimeInterval timeDifferenceBetweenDates = [shiftDateFieldObj timeIntervalSinceDate:currentDateObj]; 

您也可以根据date之间的时间差异获取时间间隔。 希望这可以帮助。