ChenSmile
ChenSmile

Reputation: 3441

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

Upvotes: 1

Views: 125

Answers (3)

Rohit Khandelwal
Rohit Khandelwal

Reputation: 1778

try this-

NSString *dateInString=@"11:15";// your OpeningTime

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

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

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

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

NSDate *Breaktime = [dateFormatter dateFromString:@"14:00"] ; // your breakStartTime

NSDate *BreaktimeEnd = [dateFormatter dateFromString:@"15:00"] ; // your breakEndTime

NSDate *closeTime = [dateFormatter dateFromString:@"20:30"] ; // your closeTime

while ([[dateFormatter dateFromString:dateInString] compare:closeTime] == NSOrderedAscending) {

    NSDate *myDate = [dateFormatter dateFromString:dateInString] ;

    if([myDate compare:Breaktime] == NSOrderedDescending && [myDate compare:BreaktimeEnd] == NSOrderedAscending){

    }else{
        if ([myDate compare:Breaktime]==NSOrderedSame) {

        }else{
            [array addObject:dateInString];
        }
    }

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

}

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

OP:-

your data :(
"10:30",
"11:00",
"11:30",
"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"
)

Upvotes: 2

Droppy
Droppy

Reputation: 9721

As per my answer to your previous question, I would take the following approach. You do not want to be comparing strings for time data:

  1. Convert all string hh:mm times into integer minutes.
  2. Iterate from the opening time to the closing time (minus appointment slot time) in steps of appointment slot time.
  3. If the time is between break start and end then ignore.
  4. Convert each value to an hh:mm string and add it to an array.

    + (int)minutesFromHhmm:(NSString *)hhmm
    {
        int minutes = 0, hh, mm;
        if (sscanf([hhmm UTF8String], "%d:%d", &hh, &mm) == 2)
            minutes = (hh * 60) + mm;
        return minutes;
    }
    
    + (NSString *)hhmmFromMinutes:(int)minutes
    {
        int hh = minutes / 60;
        int mm = minutes - (hh * 60);
        hh %= 24;    // day roll-over
        return [NSString stringWithFormat:@"%02:%02d", hh, mm];
    }
    
    - (NSMutableArray *)yourMethod:(NSDictionary *)jsonData
    {
        // 1.
        int minAppointmentTime = [self minutesFromHhmm:jsonData[@"MinPerAppointment"]];
        int openingTime = [self minutesFromHhmm:jsonData[@"OpeningTime"]];
        int breakStartTime = [self minutesFromHhmm:jsonData[@"BreakStartTime"]];
        int breakEndTime = [self minutesFromHhmm:jsonData[@"BreakEndTime"]];
        int closingTime = [self minutesFromHhmm:jsonData[@"ClosingTime"];
    
        NSMutableArray *appointmentSlots = [NSMutableArray new];
    
        // 2.
        for (int time = openingTime; time <= closingTime - minAppointmentTime; time += minAppointmentTime) {
            if (time >= breakStartTime && time <= breakEndTime)
                continue;      // 3.
            [appointmentSlots addObject:[self hhmmFromMinutes:time]];   // 4.
        }
    
        return appointmentSlots;
    }
    

Upvotes: 1

remyr3my
remyr3my

Reputation: 778

 NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat: @"yyyy-MM-dd"];

    NSDate * dt1 ;

    NSDate * dt2 ;

    dt1 = [df dateFromString: txt1.text];
    dt2 = [df dateFromString: txt2.text];

    NSComparisonResult result = [dt1 compare: dt2];

    switch (result) {
        case NSOrderedAscending:
            NSLog(@"%@ is greater than %@", dt2, dt1);
            break;
        case NSOrderedDescending:
            NSLog(@"%@ is less %@", dt2, dt1);
            break;
        case NSOrderedSame:
            NSLog(@"%@ is equal to %@", dt2, dt1);
            break;
        default:
            NSLog(@"erorr dates %@, %@", dt2, dt1);
            break;
    }

Upvotes: -1

Related Questions