比较ios中的两个时间值?

时间:2013-12-16 06:20:06

标签: ios time nsstring nsdate

在我的应用程序中,我想检查当前时间是否在变量中保存的时间之前或之后。

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

所以我想比较time1和time2。目前这些变量都是NSString格式。

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

CODE:

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

请帮帮我

4 个答案:

答案 0 :(得分:41)

使用以下代码执行从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");
}

答案 1 :(得分:5)

// 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");

答案 2 :(得分:1)

您可以使用此link

根据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 Documentation。如果您有任何问题,也可以使用此链接here。在这里你可以通过Nelson Brian回答。

我在我的结尾处完成了以下操作 -

NSDate  * currentDateObj=@"firstDate";
NSDate  * shiftDateFieldObj=@"SecondDate";


NSTimeInterval timeDifferenceBetweenDates = [shiftDateFieldObj timeIntervalSinceDate:currentDateObj];

您还可以根据日期之间的时差获取时间间隔。 希望这会有所帮助。

答案 3 :(得分:1)

首先,您需要将时间字符串转换为NSDate => Converting time string to Date format 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
       }