如何比较两个时隙之间的当前时间

时间:2013-05-24 11:40:52

标签: iphone ios nspredicate

我有这样的NSDictionary阵列。

 {
    id = 12;
    "time_from" = "12:00";
    "time_to" = "16:00";
    "timing_name" = Lunch;
},
    {
    id = 11;
    "time_from" = "08:00";
    "time_to" = "12:00";
    "timing_name" = BreakFast;
},
    {
    id = 13;
    "time_from" = "09:00";
    "time_to" = "10:00";
    "timing_name" = Dinner;
}

假设我当前的时间是8:45,那么如果当前时间在time_from和time_to之间退出,我们如何使用谓词找到精确的字典。

实际上我需要一个谓词。

4 个答案:

答案 0 :(得分:1)

你可以试试这个,

NSMutableArray *testArray = [NSMutableArray array];
    NSMutableDictionary *testDict = [NSMutableDictionary dictionary];
    [testDict setObject:@"12:45" forKey:@"time-from"];
    [testDict setObject:@"14:45" forKey:@"time-to"];
    [testArray addObject:testDict];

   NSPredicate *timePredicate = [NSPredicate predicateWithBlock:^BOOL(NSMutableDictionary *dict, NSDictionary *bindings) {

        NSString *timeFromString = [dict objectForKey:@"time-from"];
        NSString *timeToString = [dict objectForKey:@"time-from"];

        // find minutes from 00-0
        //get current time minutes from 00-00
        return (currentTimeMinutes>startimeMinutes) && (currentTimeMinutes<=endtimeMinutes)


    }];

    [testArray filterUsingPredicate:timePredicate];

答案 1 :(得分:1)

尝试使用这个......

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
    NSInteger currHr = [components hour];
    NSInteger currtMin = [components minute];

    NSString *startTime = @"10:00";
    NSString *endTime = @"16:00";

    int stHr = [[[startTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    int stMin = [[[startTime componentsSeparatedByString:@":"] objectAtIndex:1] intValue];
    int enHr = [[[endTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    int enMin = [[[endTime componentsSeparatedByString:@":"] objectAtIndex:1] intValue];

    int formStTime = (stHr*60)+stMin;
    int formEnTime = (enHr*60)+enMin;

    int nowTime = (currHr*60)+currtMin;

    if(nowTime >= formStTime && nowTime <= formEnTime)
    {
        // Do Some  Stuff..
    }

答案 2 :(得分:1)

-(void)makeCalculation
{
NSMutableDictionary *temp=[[NSMutableDictionary alloc]init];

[temp setObject:@"13" forKey:@"id"];
[temp setObject:@"12:00" forKey:@"timeFrom"];
[temp setObject:@"16:00" forKey:@"timeTo"];

NSMutableDictionary *temp1=[[NSMutableDictionary alloc]init];

[temp1 setObject:@"13" forKey:@"id"];
[temp1 setObject:@"08:00" forKey:@"timeFrom"];
[temp1 setObject:@"12:00" forKey:@"timeTo"];

NSMutableDictionary *temp2=[[NSMutableDictionary alloc]init];

[temp2 setObject:@"13" forKey:@"id"];
[temp2 setObject:@"09:00" forKey:@"timeFrom"];
[temp2 setObject:@"10:00" forKey:@"timeTo"];

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

[arrayOfDict addObject:temp];
[arrayOfDict addObject:temp1];
[arrayOfDict addObject:temp2];


NSMutableArray *timeToCompare=[[NSMutableArray alloc]initWithObjects:@"8:45",@"8:45", nil];

for(int k=0;k<[arrayOfDict count];k++)
{
    NSMutableDictionary *temp=[arrayOfDict objectAtIndex:k];

    NSString *timeFrom=[temp objectForKey:@"timeFrom"];
    NSString *timeTo=[temp objectForKey:@"timeTo"];

    NSMutableArray *tempTocheck=[[NSMutableArray alloc]initWithObjects:timeFrom,timeTo, nil];
    if([self isTimeClash:timeToCompare compareWith:tempTocheck])
    {
        NSLog(@"Time Belong in id:%@",[temp objectForKey:@"id"]);

    }
}
}


-(BOOL)isTimeClash:(NSMutableArray *)timeToCheck compareWith:(NSMutableArray *)timeToCompare
{

int Start1=[self getMinitesFromTime:[timeToCheck objectAtIndex:0]];
int end1=[self getMinitesFromTime:[timeToCheck objectAtIndex:1]];

int start2=[self getMinitesFromTime:[timeToCompare objectAtIndex:0]];
int end2=[self getMinitesFromTime:[timeToCompare objectAtIndex:1]];

if(start2>=end1 || end2<=Start1)
{
    NSLog(@"Clash not happeed");
    return NO;
}
else
{
    NSLog(@"clash hapens ");

    return YES;
}

}


-(int)getMinitesFromTime:(NSString *)timeINHours
{
NSInteger hrVal1 = [(timeINHours) intValue] ;

NSArray *array1 = [timeINHours  componentsSeparatedByString:@":"];
int minVal1 = [[array1 lastObject]intValue];

hrVal1 = hrVal1 *60;
minVal1 = hrVal1 + minVal1;


return minVal1;
}

答案 3 :(得分:1)

数组计时包含数组。您可能希望以24小时格式包含时间以保持一致性。

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

NSDate *now = [NSDate date];

__block NSString *status = nil;

[timings enumerateObjectsUsingBlock:^(NSDictionary * dict, NSUInteger idx, BOOL *stop)
 {

     NSDate *date1 = [dateFormatter dateFromString:dict[@"time_from"]];
     NSDate *date2 = [dateFormatter dateFromString:dict[@"time_to"]];

     NSDateComponents *date1Components = [[NSCalendar currentCalendar]components:NSHourCalendarUnit|NSMinuteCalendarUnit
                                                                        fromDate:date1];

     NSDateComponents *date2Components = [[NSCalendar currentCalendar]components:NSHourCalendarUnit|NSMinuteCalendarUnit
                                                                        fromDate:date2];

     NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar]components:NSHourCalendarUnit|NSMinuteCalendarUnit
                                                                              fromDate:now];

     NSInteger fromMinutes = date1Components.hour*60+date1Components.minute;
     NSInteger toMinutes = date2Components.hour*60+date2Components.minute;
     NSInteger nowMinutes   = nowDateComponents.hour*60+nowDateComponents.minute;

     if (nowMinutes>fromMinutes && nowMinutes<toMinutes) {
         status = dict[@"timing_name"];
     }

 }];

NSLog(@"%@",status);