0

starttime= 23:00 pm end time= 5:00 am

I need the summing time of the start time to end time i am trying this code but it is not helpful to me

NSDateFormatter *formatter = [NSDateFormatter new];
        [formatter setDateFormat:@"HH"];

        NSDate *currentTime = [NSDate date];
        NSDate *openTime = [formatter dateFromString:start];
        NSDate *closeTime = [formatter dateFromString:end ];
        NSCalendar *calender = [NSCalendar currentCalendar];
        NSDateComponents *currentTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:currentTime];
        NSDateComponents *openTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:openTime];
        NSDateComponents *closeTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:closeTime];

        NSUInteger currentTimeSeconds = currentTimeComponents.hour;

        NSUInteger openTimeSeconds = openTimeComponents.hour;

        NSUInteger closeTimeSeconds = closeTimeComponents.hour;

        if (currentTimeSeconds>openTimeSeconds && currentTimeSeconds<closeTimeSeconds) {
            NSLog(@"Time Between Open and Close Time %lu",(unsigned long)currentTimeSeconds);
        }

and it shows 0 hors. please help me some one? thanks in advance

1 Answers1

0

If possible, I'd look at using libs/frameworks to accomplish that through their APIs. For example, I've done date operations using DateTools

Luis Delgado
  • 3,644
  • 4
  • 34
  • 54