1

I have two NSDates, start date and end date. I want to find all the dates between start and end dates. I tried the code below, but I get the following error:

-[__NSDate length]: unrecognized selector sent to instance 0xa484b30

self.m_maEventsForMonth = [NSMutableArray array];
NSPredicate * sPredicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", start, end];
[self.m_maEventsForMonth setArray:[m_maEvents filteredArrayUsingPredicate:sPredicate]];

I also tried making start and end strings as follows, but I get the wrong results:

NSString * sDateStart = [self dateToString:start format:@"M/d/yyyy"];
NSString * sDateEnd = [self dateToString:end format:@"M/d/yyyy"];

self.m_maEventsForMonth = [NSMutableArray array];
NSPredicate * sPredicate = [NSPredicate predicateWithFormat:@"(date BETWEEN %@)", @[sDateStart, sDateEnd]];

[self.m_maEventsForMonth setArray:[m_maEvents filteredArrayUsingPredicate:sPredicate]];

I do not understand what is wrong. Any help would be appreciated.

UPDATE:

My m_maEvents is an NSMutableArray with my custom EventObjects defined as follows:

@interface EventObject : NSObject
{
    NSString    * title;
    NSString    * date;
    NSString    * starttime;
    NSString    * endtime;
    NSString    * location;
    NSString    * url;
    NSMutableArray  * m_EventTypes;
    NSDate      * dtDate;
}

@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * date;
@property (nonatomic, retain) NSString * starttime;
@property (nonatomic, retain) NSString * endtime;
@property (nonatomic, retain) NSString * location;
@property (nonatomic, retain) NSString * url;
@property (strong, nonatomic) NSMutableArray * m_EventTypes;
@property NSDate * dtDate;

I initialize these objects with the strings and in the init method create the NSDate Object. None of this really matters, my problem is that i do not know how to define the predicate to filter dates between two dates. I have the NSDate Objects and the strings at my disposal but am not sure how to define the predicate. I think it compares using the strings and that is why I get the length error.

Do I need a custom compare method? If so, I have not done one.

LilMoke
  • 3,176
  • 7
  • 48
  • 88

2 Answers2

0

Hope it works for you:

NSMutableArray dates = [NSMutableArray array];
NSDate *curDate = startDate;
while([curDate timeIntervalSince1970] <= [endDate timeIntervalSince1970]) //you can also use the earlier-method
{
    [dates addObject:curDate];
    curDate = [MSDate dateWithTimeInterval:86400 sinceDate:curDate];
}

(Please note that: 86400 = 60*60*24 (1 day)).

Hope it works for you.

iCreative
  • 1,499
  • 1
  • 9
  • 22
  • Although that is interesting, it really does not solve my problem. I have an array of dates, m_maEvents. I need to extract from that an array (m_maEventsForMonth) of the dates between a start and end date. – LilMoke Jan 07 '13 at 14:53
0

My bad... all I needed was to use the right attribute in the predicate statement. The right attribute is: dtDate, not date.

LilMoke
  • 3,176
  • 7
  • 48
  • 88