-2

I want to compare current time with two different times on different dates.MY code is

NSString *time1 = record.trigger_from_time;
NSString *time2 = record.trigger_to_time;

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];

NSDate *date1= [formatter dateFromString:time1];
NSDate *date2 = [formatter dateFromString:time2];

NSLog(@"%@,%@",date1,date2);

I'm getting log as 2000-01-01 09:12:00 +0000,2000-01-01 12:30:00 +0000

But MY current time is 2014-04-01 17:06:18.

I'm getting current date from

NSDate *now = [NSDate date]; 
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; 
timeFormatter.dateFormat = @"HH:mm"; 
[timeFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
NSLog(@"The Current Time is %@",[timeFormatter stringFromDate:now]); 

I want to compare thie current time with record.trigger_from_time and record.trigger_to_time

user2864740
  • 60,010
  • 15
  • 145
  • 220
Aruna M
  • 386
  • 3
  • 12
  • I am not sure what your question has to do with the code you posted – Marc Apr 01 '14 at 11:40
  • 2
    what is record.trigger_from_time? and what is record.trigger_to_time? – Adam Richardson Apr 01 '14 at 11:44
  • I want to get date1 and date2 time as time with current date.I can do coparision but date is showing as 2000-01-01 not getting current date. – Aruna M Apr 01 '14 at 11:48
  • Can you share the code where you get the current date as record.trigger_from_time isn't the current date unless you are setting that somewhere else this looks like something from an object – Adam Richardson Apr 01 '14 at 11:52
  • record.trigger_from_time and record.trigger_to_time are time string coming from Json Api.record.trigger_from_time="18:00:00" and record.trigger_to_time= "06:07:00" – Aruna M Apr 01 '14 at 11:53
  • I'm getting current date from NSDate *now = [NSDate date]; NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; timeFormatter.dateFormat = @"HH:mm"; [timeFormatter setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"The Current Time is %@",[timeFormatter stringFromDate:now]); I want to compare thie current time with record.trigger_from_time and record.trigger_to_time – Aruna M Apr 01 '14 at 11:56
  • You need to get everything on the same page (of the calendar). Either convert current time to Day 1 or convert your timestamps to today. (Or better still, save the timestamps as valid NSDate values to begin with.) – Hot Licks Apr 01 '14 at 12:05
  • Hint: Someone who was fairly skilled at Objective-C could avoid it, but use NSCalendarComponents to separate your values and recombine them so everything is on the same day. Will be more than a few lines, but it's straight-forward code. – Hot Licks Apr 01 '14 at 12:07
  • Oops, I mean "NSDateComponents". – Hot Licks Apr 01 '14 at 12:12
  • please provide some code with NSCalendarComponents.I'm very new to this technology. – Aruna M Apr 01 '14 at 12:14
  • please provide some code with NSDateComponents.I'm very new to this technology – Aruna M Apr 01 '14 at 12:15
  • Google it. And look at the spec. (You do know how to find the spec, right?) – Hot Licks Apr 01 '14 at 15:16

2 Answers2

2
NSString *time1 = @"09:10:02";
NSString *time2 = @"17:10:16";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:"];

NSDate *date1= [formatter dateFromString:time1];
NSDate *date2 = [formatter dateFromString:time2];

NSComparisonResult result = [date1 compare:date2];
if(result == NSOrderedDescending)
{
     NSLog(@"time1 is later than time2"); 
}
else if(result == NSOrderedAscending)
{
     NSLog(@"time2 is later than time1"); 
}
else
{
     NSLog(@"time1 is equal to time2");
}
1

Comparing 2 NSDates.

    ...
    NSDate *date1= [formatter dateFromString:time1];
    NSDate *date2 = [formatter dateFromString:time2];

    if ([date2 compare:date1] == NSOrderedDescending) {
        NSLog(@"date1 is later than date2");

    } else if ([date2 compare:date1] == NSOrderedAscending) {
        NSLog(@"date1 is earlier than date2");

    } else {
        NSLog(@"dates are the same");

    }

UPD: via Switch

       ...
        NSDate *date1= [formatter dateFromString:time1];
        NSDate *date2 = [formatter dateFromString:time2];

        NSComparisonResult result = [date2 compare:date1];
        switch(result){
            case NSOrderedDescending:
                NSLog(@"date1 is later than date2");
                break;
            case NSOrderedAscending:
                NSLog(@"date1 is earlier than date2");
                break;
            default:
                NSLog(@"dates are the same");
                break;
         }
Zhans
  • 273
  • 2
  • 11