I've been searching this and couldn't quite find it. I have an object in a NSDictionary that contains a NSDate. Now standard NSDate objects are pretty long. and I want to show the user in dd-MMM format.
For eg: The original date may be 2012-04-23 00:00:00 +0000
, for which I want the date to appear as 23 Apr
I tried using NSDateComponents and NSDateFormatter but it didn't quite work. Probably I'm not figuring out the correct usage FOR the required format.
Here's the code for better understanding:
NSLog(@"From Dictionary, Date = %@",[tmpDict objectForKey:@"eventDate"]);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateString = [tmpDict objectForKey: @"eventDate"];
NSDate *date = [dateFormatter dateFromString: dateString];
NSLog(@"MY DATE: %@",date);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd-MMM"];
NSString *formattedDateString = [dateFormatter2 stringFromDate:date];
NSLog(@"Formatted Date: %@",formattedDateString);
The output is 2012-05-19 07:30:56 for From Dictionary, Date
and null for Formatted Date and MY DATE. And tmpDict
is the Dictionary Object that I'm using.
Thanks for the help, in advance.