Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two time values in ios? [duplicate]

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

like image 930
Bangalore Avatar asked Dec 16 '13 06:12

Bangalore


3 Answers

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");
}
like image 178
chancyWu Avatar answered Nov 07 '22 03:11

chancyWu


// 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");
like image 5
Rajneesh071 Avatar answered Nov 07 '22 03:11

Rajneesh071


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
       }
like image 1
iPatel Avatar answered Nov 07 '22 03:11

iPatel