如何比较ios中的时间大于3秒。 时间就像“2016-05-10 05:31:14”。我得到的代码用于比较更大或更小的特定时间,但我将如何实现它大于3秒等。
答案 0 :(得分:1)
您可以通过
获得两个日期之间的秒差NSDate *someDate;//Some Date
NSLog(@"Seconds --> %f",[[NSDate date] timeIntervalSinceDate: someDate]);
答案 1 :(得分:0)
请尝试这个。可能对你有用:
NSDate* timeNow = [NSDate date];
// If less than 30 seconds, do something
if ([timeNow timeIntervalSinceDate:anEarlierTime] < 30.0f)
{
// Do something
}
答案 2 :(得分:0)
- (BOOL)isEndDateIsSmallerThanCurrent:(NSDate *)checkEndDate
{
NSDate* enddate = checkEndDate;
NSDate* currentdate = [NSDate date];
NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
double secondsInMinute = 60;
// secondsBetweenDates is your seconds difference
NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;
if (secondsBetweenDates == 0)
return YES;
else if (secondsBetweenDates < 0)
return YES;
else
return NO;
}
这里,secondsBetweenDates是你的秒数差异。您可以检查它是否小于或大于或等于3.
你也可以在几小时内得到改变!
希望这会有所帮助:)