-5

I'd like to compare two different times in an if statement. Fort example

if (10:37 > 09:00) // DO SOMETHING

Is there a way to do this using NSDate and NSDateFormatter?

user3626407
  • 287
  • 1
  • 6
  • 15
  • 1
    Yes, you can do it by reading the documentation, or, if you're too lazy to do that, using Google. – Hot Licks Jul 16 '14 at 16:19
  • See http://stackoverflow.com/questions/7370123/how-to-compare-time-with-current-date-and-time for an answer that should help. – rmaddy Jul 16 '14 at 16:24
  • 1
    In what format do you have the two times? Are they time stings? Full `NSDate` objects? What? The answer depends on this information. – rmaddy Jul 16 '14 at 16:40
  • Please refer http://stackoverflow.com/questions/5388551/how-to-compare-time – TechSavy Dec 22 '15 at 07:07

5 Answers5

0

You need to use timeIntervalSinceDate api for comparing two different times.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
0
NSDate *date1;
NSDate *date2;   
if([date1 laterDate:date2] == *it will return the later date, either date1 or date2*)
washloops
  • 147
  • 3
  • This will only work if the two dates happen to be on the same day. – rmaddy Jul 16 '14 at 16:40
  • Nope. I use this for any date. Please inform yourself before commenting – washloops Jul 16 '14 at 16:43
  • My point is that the OP seems to only want to compare the two time portions of the two `NSDate` objects. Your check will not work if the two dates are on different days. Actually it will work sometimes but not for all cases. – rmaddy Jul 16 '14 at 16:46
  • @rmaddy - The OP hasn't really defined his problem with enough precision to say whether that is right or wrong. – Hot Licks Jul 16 '14 at 21:58
0

Try this

//Date formatter - to format the sample dates using for comparison
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"HH:MM:SS"];

// Creating 2 sample dates
NSDate *date1 = [dateformatter dateFromString:@"10:10:10"];
NSDate *date2 = [dateformatter dateFromString:@"10:10:20"];

if (NSOrderedDescending == [date1 compare:date2])// Here comparing the dates 
{
    NSLog(@"date1 later than date 2");
}
else
{
    NSLog(@"date2 later than date 1");
}
Alex Andrews
  • 1,498
  • 2
  • 19
  • 33
  • The OP wants to check just the times, not the full date. – rmaddy Jul 16 '14 at 16:46
  • An NSDate object holding a timestamp no? Then what is the problem if using this? I tested with 2 sample NSDate objects created with date format [dateformatter setDateFormat:@"HH:MM:SS"]; And it's working for me well. – Alex Andrews Jul 16 '14 at 16:58
  • 1
    Your answer should show that info. It doesn't work for two arbitrary dates but it works if dome as you describe in the comment. Put that info in your answer. – rmaddy Jul 16 '14 at 17:11
  • Updated the answer. Please check. I just added only the relevant part before – Alex Andrews Jul 16 '14 at 17:21
  • That looks good except you should never set both the dateStyle and the date format of a date formatter. And we have no idea if that is what the OP needs. The OP told us nothing about that they actually have to start with. So this answer may be perfect or completely useless. – rmaddy Jul 16 '14 at 17:24
0

As I understand your question, you only have times, not dates. Actually than NSDate isn't the best choice, as it represents exactly one moment in time for all eternity.

If you only have times or recurring dates, NSDateComponents are the better choice. You can define what components is used, in your case hour and minute.

NSDateComponents have one caveat: no compare: method, but you can easily compare as

if (date1Comps.hour < date2Comps.hour) {
    NSLog(@"date 1 earlier");
} else if(date1Comps.hour > date2Comps.hour) {
    NSLog(@"date 2 earlier");

} else {
    if (date1Comps.minute < date2Comps.minute) {
        NSLog(@"date 1 earlier");
    } else if(date1Comps.minute > date2Comps.minute){
        NSLog(@"date 2 earlier");
    } else {
        NSLog(@"same hour, same minute");
    }
}

here is the full code including creating components from dates.

NSString *date1String = @"2014-07-18T10:30";
NSString *date2String = @"2014-07-18T10:45";

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
NSDate *date1 = [f dateFromString:date1String];
NSDate *date2 = [f dateFromString:date2String];

NSDateComponents *date1Comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour|NSCalendarUnitMinute) fromDate:date1];

NSDateComponents *date2Comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour|NSCalendarUnitMinute) fromDate:date2];

if (date1Comps.hour < date2Comps.hour) {
    NSLog(@"date 1 earlier");
} else if(date1Comps.hour > date2Comps.hour) {
    NSLog(@"date 2 earlier");

} else {
    if (date1Comps.minute < date2Comps.minute) {
        NSLog(@"date 1 earlier");
    } else if(date1Comps.minute > date2Comps.minute){
        NSLog(@"date 2 earlier");
    } else {
        NSLog(@"same hour, same minute");
    }
}

or similar: order the date components in an array

NSArray *times = [@[date1Comps, date2Comps] sortedArrayUsingComparator:^NSComparisonResult(NSDateComponents *date1Comps, NSDateComponents *date2Comps) {
    if (date1Comps.hour < date2Comps.hour) {
        return NSOrderedAscending;
    } else if(date1Comps.hour > date2Comps.hour) {
        return NSOrderedDescending;
    } else {
        if (date1Comps.minute < date2Comps.minute) {
            return NSOrderedAscending;
        } else if(date1Comps.minute > date2Comps.minute){
            return NSOrderedDescending;
        }
    }
    return NSOrderedSame;
}];

You can save this comparison block and reuse it. Than it won't matter much that there is no easy compare method.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger todayHour = [components hour];
NSInteger currentMinute = [components minute];
if (currentHour < 7 || (currentHour > 21 || currentHour == 21 && (currentMinute > 0 || currentMinute > 0))) {
// Do your task
}

This may helps you :)

Anjali jariwala
  • 410
  • 5
  • 15