Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Date Filters in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 437
    Comment on it

    Hi, iPhone developers today we will see how to fetch records lying between 2 dates in iOS i.e basically applying date filters on records of data. The first thing we have to take care is the "format" of records we have in our database.
    In Case the records we have & records we need to display different formats then we can always change formats of the records by using NSDateFormatter as-

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

     

    Now Let's jump to the code.

    Firstly what we have to do is to create a Predicate or NSPredicate *predicate; Predicate is simply a parameter or value on the basis of which the comparison is done. So u have to define the predicate. Here is the complete code snippet in which I have defined Date Format, Set NSpredicate & stored the filtered results in an array.

    -(void)startFilterByDate{
        
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd - MM -yyyy"];
       
    
        NSDate *date1 = [dateFormatter dateFromString:startDateLBL.text];
        NSDate *date2 = [dateFormatter dateFromString:endDateLBL.text];
        dateFormatter.dateFormat = @"dd/MM/yyyy";
     
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"filteredPaymentDate >=  %@ AND filteredPaymentDate <=  %@",date1,date2];
        filter = [DateRecords filteredArrayUsingPredicate:predicate];
        NSLog(@"new filtered array== %@",filter);
    
    }

    Now in this function we have two dates date1 & 2  of NSDate Format Date1 refers to Start date & Date2 refers to End date, filter is an NSArray in which we have to store filtered results from complete data & DateRecords is array of all the random date from which we are filtering whereas startDateLBL & endDateLBL are the two labels in which we select the dates from which we have to filter the results. So all you have to do is set your variables array according to your data, call this function & filtered results will be available to you.

    Thanks for reading.

    hope its useful for you.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: