1
votes

Date that i am receiving from internet is of the format like:

"2014-12-01T20:00:00Z"

i am Storing date with this format in core data..Attribute type is also "Nsdate" type..

- (NSDateFormatter *)dateFormate
{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy'-'dd'-'MM'T'HH':'mm':'ssZZZ"];
}

Getting start time of [NSDate Date] by using

- (NSDate *)cc_dateByMovingToBeginningOfDay
{
  unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
  NSDateComponents* parts = [[NSCalendar currentCalendar] components:flags fromDate:self];
  [parts setHour:0];
  [parts setMinute:0];
  [parts setSecond:0];
  return [[NSCalendar currentCalendar] dateFromComponents:parts];
}

and end time of [NSDate Date] by using:

- (NSDate *)cc_dateByMovingToEndOfDay
{
  unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
  NSDateComponents* parts = [[NSCalendar currentCalendar] components:flags fromDate:self];
  [parts setHour:23];
  [parts setMinute:59];
  [parts setSecond:59];
  return [[NSCalendar currentCalendar] dateFromComponents:parts];
}

and fetching with this one:

- (void)ObjectsFrom:(NSDate *)fromDate to:(NSDate *)toDate {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((due_at > %@) AND (due_at < %@))",fromDate, toDate];
}

But record is always zero.as i know that in my today date i have multiple records in data base..i dont know why this happens..

i have also used NSComparisonResult for dates But no record is Between date.

SO please help me out in this..

1
i have done NSComparisonResult like this way but it fails as well.. static BOOL IsDateBetweenInclusive(NSDate *date, NSDate *begin, NSDate *end) { return [date compare:begin] != NSOrderedAscending && [date compare:end] != NSOrderedDescending; }Umair Suraj

1 Answers

1
votes

Your date format is wrong. The "Z" in "2014-12-01T20:00:00Z" is a "ISO8601 time zone format", and that requires "ZZZZZ" in the date format:

[formatter setDateFormat:@"yyyy'-'dd'-'MM'T'HH':'mm':'ssZZZZZ"];

otherwise dateFromString: will return nil.

See http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns for more information.