-2

Possible Duplicate:
NSString to NSDate

I am developing one application in that i am try to convert the NSString value to NSDate.I got the result like date:2011-11-23 18:30:00 +0000. But I want result like 2011-11-24 only.

At the time of conversion date value is also changed. for example in string value is 2011-11-24, then convert into NSDate value is 2011-11-23 18:30:00 +0000.

Please tell me how to solve this one.

My Sample Code:

   +(BOOL)checkdate:(KLDate*)dbDate
    {
    NSString *query = [NSString stringWithFormat:@"SELECT * FROM CalendarEvents"];
    sqlite3_stmt *stStatement;

    if(sqlite3_prepare_v2(database, [query UTF8String], -1, &stStatement, nil)==SQLITE_OK)
     {
       while (sqlite3_step(stStatement)==SQLITE_ROW) {

        NSString *dateString = [NSString stringWithUTF8String:    (char*)sqlite3_column_text(stStatement, 1)];

         if([dateString isEqual:dbDate])
        {
            sqlite3_close(database);
            return  YES;
        }
      }
    }
    sqlite3_close(database);
    return NO;
  }
Community
  • 1
  • 1
Istalla Raju
  • 125
  • 7
  • 1
    there are [so many](http://stackoverflow.com/questions/3738363/nsstring-to-nsdate-conversion-problem) [duplicate questions](http://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again) on this F.A.Q., I don't know where to begin... – Michael Dautermann Nov 25 '11 at 06:21
  • if([dateString isEqual:dbDate]) in this line of code i compare NSString and user defined Class object. How can i compare these two?Please tell me. – Istalla Raju Nov 25 '11 at 07:02

2 Answers2

1

This is the right way

           NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
           [dateFormat setDateFormat:@"yyyy-dd-MMM"];
           NSString *dateString = [dateFormat stringFromDate:today];
           NSLog(@"date: %@", dateString);
           [dateFormat release];

For your code instead of dateString pass your string....

Minakshi
  • 1,437
  • 1
  • 11
  • 19
0

please change your date format to like this -

[format setDateFormat:@"yyyy-MM-dd"];
saadnib
  • 11,145
  • 2
  • 33
  • 54
  • if([dateString isEqual:dbDate]) in this line of code i compare NSString and user defined Class object. How can i compare these two?Please tell me. – Istalla Raju Nov 25 '11 at 07:07