0

I'm parsing an array and want to weed out records from before now.

I've got this code:

int i = 0;
    for (i=0; i < tempArray.count; i++) {
        currentObjectArray = tempArray[i];
        NSString *dateString = [currentObjectArray valueForKey:@"ScheduleTime" ];
        NSDate *schedule = [dateFormatter dateFromString:dateString];
        NSLog(@"schedule: %lu", (unsigned long) schedule );
        NSLog(@"now: %lu", (unsigned long)[NSDate date] );

        NSTimeInterval distanceBetweenDates = [schedule timeIntervalSinceDate: schedule];

NSLog(@"distanceBetweenDates: %lu", (unsigned long)distanceBetweenDates );

result:

 schedule: 16436914033316069376
 now: 6174145184
 distanceBetweenDates: 0

but the two resulting numbers are incorrect, thus the result is incorrect. Could someone please tell me what I'm doing wrong? Thanks

UPDATE: Thanks to answers below, I've updated my code as follows:

NSString *dateString = [currentObjectArray valueForKey:@"ScheduleTime" ];
        NSDate *schedule = [dateFormatter dateFromString:dateString];

        float s = [schedule timeIntervalSince1970];
        NSLog(@" %f", s );

        NSTimeInterval timeInterval = [currentObjectArray timeIntervalSinceNow];
        if (timeInterval > 0) {
            NSLog(@"YES");
        } else {
            NSLog(@"NO");

The schedule date format is: "YYYY-MM-DD'T'HH:mm:ss"

Update2: I forgot to add in the local time zone. Thanks for all the help.

ICL1901
  • 7,632
  • 14
  • 90
  • 138

2 Answers2

1

These two lines don't do what you think they do.

NSLog(@"schedule: %lu", (unsigned long) schedule );
NSLog(@"now: %lu", (unsigned long)[NSDate date] );

Performing this type cast is asking the system to return you an unsigned long representation of the pointer to the object, which is a memory address and not at all related to time. It is likely that you actually wanted to ask for the NSTimeInterval values.

NSLog(@"schedule: %f", [schedule timeIntervalSince1970] );
NSLog(@"now: %f", [[NSDate date] timeIntervalSince1970] );

Compounding your confusion, you have also misunderstood this line:

NSTimeInterval distanceBetweenDates = [schedule timeIntervalSinceDate: schedule];

You are asking the system to tell you how many seconds are between schedule and schedule; which is obviously always going to be 0 since they are identical. Instead, you probably meant one of:

NSTimeInterval distanceBetweenDates1 = [[NSDate date] timeIntervalSinceDate:schedule];
NSTimeInterval distanceBetweenDates2 = [schedule timeIntervalSinceDate:[NSDate date]];
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
1

You only need to check if the time interval is negative or positive to determine if a time comes before or after, respectively.

- (BOOL)isDateInPast:(NSDate *)date {
    NSTimeInterval timeInterval = [date timeIntervalSinceNow];
    if (timeInterval < 0) {
        return YES;
    } else {
        return NO;
    }
}

Note that this doesn't check the condition where the time interval is 0 (the present).

EDIT: Adding to this for further clarification. Your loop code could look something like this...

NSMutableArray *datesOnlyInFuture = [NSMutableArray array];
for (NSDate *date in dateArray) {
    if (![self isDateInPast:date]) {
        [datesOnlyInFuture addObject:date];
    }
}
NSLog(@"Future only dates: %@", datesOnlyInFuture);

This will actually create a new array for you. Clearly plenty of optimizations should be made. For example timeIntervalSinceNow is going to be different each time it is called, so you could pass in a constant date that is set before the loop starts so you're always checking against the same date/time.

timgcarlson
  • 3,017
  • 25
  • 52
  • I'm updating my question as the numbers are closer but "now" is way ahead of schedule.. So nothing is being selected... – ICL1901 Feb 11 '15 at 20:55