如何获取String形式的2个String值之间的时间

时间:2016-07-13 10:23:39

标签: ios objective-c

我有工作时间JSON

outer loop/ inner loop
1,        / 2, 3,4,5,6
2,        /3, 4, 5, 6
3,        /4, 5, 6
4,        /5, 6
5,        /6

我想通过忽略BreakStartTime和BreakEndTime 将时间间隔为30(MinPerAppointment) OpeningTime 的所有数据加到 ClosingTime

我想以此格式获取数据

10:30

11:00

11:30

..在BreakStartTime到BreakEndTime ..之间被忽略的数据,即12到13:00

13:00

13:30

.....

.....

19:00

3 个答案:

答案 0 :(得分:2)

试试这个 -

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"
)

答案 1 :(得分:1)

根据您my answerprevious question,我会采取以下方法。您不希望比较时间数据的字符串:

  1. 将所有字符串hh:mm转换为整数分钟。
  2. 按照预约时段步骤从开放时间到结束时间(减去预约时间)进行迭代。
  3. 如果时间介于休息开始和结束之间,则忽略。
  4. 将每个值转换为hh:mm字符串并将其添加到数组中。

    + (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;
    }
    

答案 2 :(得分:-1)

 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;
    }