0

I have following code, where I'm comparing two string but its throwing exception.

- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d {
NSLog(@"calendarMonthView didSelectDate %@",d);
//[self papulateTable];
//[table reloadData];
 //[self performSelector:@selector(papulateTable) withObject:nil afterDelay:1.0];
NSString *tempDate = (NSString*)d;
NSString *selectedDate = @"2013-02-04 00:00:00 +0000";
if([tempDate isEqualToString:selectedDate])
{
  flagtoCheckSelectedCalendarDate = 1;
}
if(flagtoCheckSelectedCalendarDate == 1)
{
    [self viewDidLoad];
}
if(flagtoCheckSelectedCalendarDate == 2)
{
    [self viewDidLoad];
}
//[table reloadData];

}

Could any one please suggest.Thanks.

Luke
  • 11,426
  • 43
  • 60
  • 69
bapi
  • 1,903
  • 10
  • 34
  • 59

4 Answers4

2

Casting an NSDate object to NSString does not make it a string. To compare dates, you're going to have to transform the NSString into an NSDate using an NSDateFormatter. After that, you can use NSDate's instance method isEqualToDate: for your comparison.

NSString *selectedDate = @"2013-02-04 00:00:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-DD hh:mm:ss ZZZZ"];
NSDate *actualDate = [dateFormatter dateFromString:selectedDate];


if ([actualDate isEqualToDate:d]) {
   ...
}
Stavash
  • 14,244
  • 5
  • 52
  • 80
  • Thanks for this code. My "d" is comming in "2013-02-04 04:30:00 IST" format.And, the compair fails.So,its not going inside if statement. Note: I'm using Calendar to selet my date. – bapi Feb 01 '13 at 11:10
  • It doesn't matter what format was used to create a date, in the end you have 2 NSDate objects that represent a date (in any format). Please explain your problem in depth. – Stavash Feb 01 '13 at 11:13
  • Problem is: after using "NSDate *actualDate = [dateFormatter dateFromString:selectedDate];" My date is showing "actualDate = 2013-01-04".How it is going back to Jan month after using dateFormatter? – bapi Feb 01 '13 at 11:41
  • Thanks for help. I just change the format to : [dateFormatter setDateFormat:@"yyyy-MM-DD hh:mm:ss zzzz"]; Because, my time zone is GMT+5.30.Now its working. – bapi Feb 01 '13 at 13:51
1

d is of type NSDate and not NSString, therefore -isEqualToString: results in a crash.
You shouldn't compare strings here, but the dates. Use NSDate's -compare: method and change

NSString *selectedDate = @"2013-02-04 00:00:00 +0000";

to

NSDate *selectedDate = [NSDate ...];
Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
  • Could you pl. tell me what should I write between "[NSDate ...];" – bapi Feb 01 '13 at 10:48
  • That depends on which date you want to compare to. You can use NSCalendar to create a date on a specific day/month/year or convert the string to a date using what @Stavash posted. – Fabian Kreiser Feb 01 '13 at 11:06
0

You are comparing NSDate and NSString. You would need to change the NSDate to string using a date formatter first.

0

You are casting an NSDate object to NSString without converting it. You will have to format the NSDate as a string into your expected date format before you can compare it with your selectedDate. See this previous answer or this one for examples

Community
  • 1
  • 1