Objective-c – String description of NSDate

cocoansdateobjective c

I have the following code:

[ [NSDate date] descriptionWithLocale: @"yyyy-MM-dd" ]

I want it to return a date in the following format: "2009-04-23"

But it returns: Thursday, April 23, 2009 11:27:03 PM GMT+03:00

What am I doing wrong?

Thank you in advance.

Best Answer

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];  // delete this line if your project uses ARC
NSLog(@"%@",dateString);