Compare two time values in ios?

20,678

Solution 1

Use the following code to do the convert from the nsstring to nsdate and compare them.

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

Solution 2

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

Solution 3

First you need to convert your time string to NSDate => Converting time string to Date format iOS.

Then use following code

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
       }

Solution 4

You can use this link

According to Apple documentation of NSDate compare:

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

In other words:

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

Note that it might be easier to read and write this :

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

See Apple Documentation about this one. If you feel any problem you can also use this link here. Here you can go through Nelson Brian answer.

I have done this at my end as follows-

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


NSTimeInterval timeDifferenceBetweenDates = [shiftDateFieldObj timeIntervalSinceDate:currentDateObj];

You can also get time interval according to time difference between dates. Hope this helps.

Share:
20,678
Bangalore
Author by

Bangalore

Updated on August 17, 2022

Comments

  • Bangalore
    Bangalore over 1 year

    In my app i want to check whether the current time is before or after the time saved in a variable.

    like my time1 is time1=@"08:15:12"; and my time2 is time2=@"18:12:8";

    so i wanna compare between time1 and time2.Currently these variables are in NSString Format.

    Following are the code used to get time,and i dont know how to compare them.

    CODE:

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

    Please help me

  • Bangalore
    Bangalore over 10 years
    but how can i convert time value to NSdate?
  • iEinstein
    iEinstein over 10 years
    Simple using NSDate Formattor Yiou can change your date according to you
  • Bangalore
    Bangalore over 10 years
    i dont have any date only time ,is that possible?
  • iEinstein
    iEinstein over 10 years
    Yeah first change it to the format it is coming after that you again set date formatter to the format you want time
  • tech savvy
    tech savvy over 9 years
    comparing this way is giving me garbage values in date1 and date2. Check my question here.. stackoverflow.com/questions/25571399/…