Imran Imran - 4 months ago 10
Objective-C Question

How to get Time between 2 String values which are in String form

I Have Shop Working Hour JSON

{
"MinPerAppointment": "30",
"OpeningTime": "10:30",
"BreakStartTime": "12:00",
"BreakEndTime": "13:00",
"ClosingTime": "19:30"
}


I want to get All Data of time Interval of 30(MinPerAppointment) OpeningTime to ClosingTime by Neglecting BreakStartTime and BreakEndTime.

I want to get in Data in this Format

10:30

11:00

11:30

..Neglected Data Between BreakStartTime to BreakEndTime.. , which is 12 to 13:00

13:00

13:30

.....

.....

19:00

Answer

try this-

NSString *dateInString=@"10:30";// your OpeningTime

NSMutableArray *array=[[NSMutableArray alloc]init];

[array addObject:dateInString];

NSString *str_MinPerAppointment = @"30"; // your MinPerAppointment

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"HH:mm"];


while (![dateInString isEqualToString:@"19:30"]) { // your ClosingTime

    if ([dateInString isEqualToString:@"12:00"]) { // your BreakStartTime


        NSDate *myDate = [dateFormatter dateFromString:dateInString] ;


        dateInString = [dateFormatter stringFromDate:[myDate dateByAddingTimeInterval:60*[str_MinPerAppointment integerValue]]];

    }else{


        NSDate *myDate = [dateFormatter dateFromString:dateInString] ;

        dateInString = [dateFormatter stringFromDate:[myDate dateByAddingTimeInterval:60*[str_MinPerAppointment integerValue]]];

        [array addObject:dateInString];
    }

}

NSLog(@"your data :%@",array);

OP:-

your data :(
    "10:30",
    "11:00",
    "11:30",
    "12:00",
    "13:00",
    "13:30",
    "14:00",
    "14:30",
    "15:00",
    "15:30",
    "16:00",
    "16:30",
    "17:00",
    "17:30",
    "18:00",
    "18:30",
    "19:00",
    "19:30"
)
Comments