0

what I am trying to do is make a if statement with dates using greater than less than signs. For some reason only the greater than sign works. Here is my code:

NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"HHmm"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(@"%@",dateString);

if (dateString < @"0810" && dateString > @"0800") {
    NSLog(@"Homeroom");
}
else {
    NSLog(@"no");
}

The output for this code would be if the time was 8:03:

2013-04-08 08:03:47.956 Schedule2.0[13200:c07] 0803
2013-04-08 08:03:47.957 Schedule2.0[13200:c07] no

If I were to make is so where it is only the greater then sign like this:

if (dateString > @"0800") {
    NSLog(@"Homeroom"); 
}
else {
    NSLog(@"no");
}

The output would be this:

2013-04-08 08:03:29.748 Schedule2.0[14994:c07] 0803
2013-04-08 08:03:29.749 Schedule2.0[14994:c07] Homeroom
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178

5 Answers5

3

create a NSDate object with the time 8:10 and one with 8:00. Now you can compare the given date with both these dates

if(([date0800 compare:date] == NSOrderingAscending) && [date0810 compare:date] == NSOrderingDescending) )
{
    // date is between the other
}

to create the boundaries dates you can do this

NSDate *date = [NSDate date]; // now
NSDateComponents *components = [[NSCalendar currentCalendar] components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:date];
components.hour = 8;
components.minute = 0;

NSDate *date0800 = [[NSCalendar currentCalendar] dateFromComponents: components];
components.minute = 10;
NSDate *date0810 = [[NSCalendar currentCalendar] dateFromComponents: components];

if you insist of using operators like < and >, you can use the timeinterval of the date objects.

if(([date0800 timeIntervalSince1970] < [date timeIntervalSince1970]) && ([date0810 timeIntervalSince1970] > [date timeIntervalSince1970]))
{
    // date lays between the other two
}

but beware of checking == on it, as it could be faulty due to rounding errors.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Can you give an example of one of the NSDate objects sorry im new to objective c... – user2259486 Apr 08 '13 at 22:58
  • please see my edit. and check apples docs for dates and calendar topics: Performing Calendar Calculations is not trivial – vikingosegundo Apr 08 '13 at 23:03
  • yes this works also! Thank you so much i was wondering if i could ask you to help me with one more thing. Is it possible to read only a certain line in a text file? For example: `NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"d.txt"]; NSString *stringFromFile = [NSString stringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil]; label.text = stringFromFile;'` – user2259486 Apr 08 '13 at 23:12
  • you should search for it, one post here: http://stackoverflow.com/questions/3707427/how-to-read-data-from-nsfilehandle-line-by-line but there are plenty of options, as you also could use standard c functions – vikingosegundo Apr 08 '13 at 23:19
1

Here you are comparing string objects, with < and >, which does not do what you are expecting. You can use NSDateComponents to get the hour and minute to compare those:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc]

                     initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components =

                [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:today];

NSInteger hour = [weekdayComponents hour];

NSInteger minutes = [weekdayComponents minute];

BOOL homeroom = (hour == 8) && (minute < 10);

Or you can create a specific NSDate for 8:10 and 8:00 using NSDateFormater and using the compare: function.

Mike Katz
  • 2,060
  • 2
  • 17
  • 25
0

NSString objects are objects, and when you compare objects with C comparison operators (==, >, <, etc.) you are comparing their addresses, not their values. You need to use compare, such as:

if ([dateString compare:@"0810"] == NSOrderedAscending &&
    [dateString compare:@"0800"] == NSOrderedDescending) { ...

Though I'd recommend converting to NSDate objects in most cases if you want to compare dates and times.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • This worked perfectly thank you so much this worked perfectly! But what do you mean converting to NSDate? – user2259486 Apr 08 '13 at 22:54
  • See https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtDates.html and https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html. NSDate, NSDateComponents, and NSDateFormatter are built-in types for managing date and time calculations and display. But glad your strings are working. – Rob Napier Apr 09 '13 at 02:09
0

You can't use > or < to compare string objects. That actually compares pointers so we won't get into why > 'works' and < 'doesn't'.

For this kind of date comparison use NSDateComponents NSDateComponents Reference

Wain
  • 118,658
  • 15
  • 128
  • 151
0

Here's the gist of a category I wrote on NSDate. I found it made my code more readable.

https://gist.github.com/nall/5341477

@interface NSDate(SZRelationalOperators)
-(BOOL)isLessThan:(NSDate*)theDate;
-(BOOL)isLessThanOrEqualTo:(NSDate*)theDate;
-(BOOL)isGreaterThan:(NSDate*)theDate;
-(BOOL)isGreaterThanOrEqualTo:(NSDate*)theDate;
@end
nall
  • 15,899
  • 4
  • 61
  • 65