views:

66

answers:

1

hi, i got a problem with my time interval. I need to get the interval of two times in this format : HH:MM. If i enter : 15:35 and 16:35 it is ok, but when i do 20:30 to 01:30, it gives me like 18 hours interval.. anyone have an idea?

        NSString *startDate= starthere.text;
    NSString *endDate = endhere.text;


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

    NSDate *dateSelected = [dateFormatter dateFromString:startDate];        
    NSDate *dateSelected2 = [dateFormatter dateFromString:endDate];
    [dateFormatter release];





        if ([dateSelected2 earlierDate:dateSelected] == dateSelected2)
        {
            dateSelected2 = [dateSelected2 dateByAddingTimeInterval:86400];
        }





interval = [dateSelected2 timeIntervalSinceDate:dateSelected];
+3  A: 

Your end date is before your start date, so there are 19 hours between them. A quick fix might be to add 24 hours to your end date if it compares as earlier than the start date. Something like:

if ([dateSelected2 earlierDate:dateSelected] == dateSelected2)
{
    dateSelected2 = [dateSelected2 dateByAddingTimeInterval:86400];
}

The NSDate documentation has everything you need to know.

Carl Norum
thx for response, how do i add 24 hour to a NSDate?
the1nz4ne
it doesnt work it tell me, NSDate may not respond
the1nz4ne
@th1nz4ne, it works for me here. Are you on an older version of OS X? If you're using something older than 10.6, you need `addTimeInterval:` instead of `dateByAddingTimeInterval:`.
Carl Norum
i got 10.6 and when i enter for time 20:30 to 01:30 it says like 1 hour..
the1nz4ne
i used add time interval and it works... its wierd i have 10.6
the1nz4ne