tags:

views:

48

answers:

1

Hi,

I have a NSString which is passed from an xml feed........

NSString *strDate =@"May 14 2010";

I'm currently using this code to format the date........

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"d:MMM"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);

I want to format my string only to display day and month and currently I'm getting a nil value to the NSDate. Can anyone please help me with this?......

Thanks.

+1  A: 

I think you're slightly misunderstanding how NSDateFormatter works. It cannot automatically work out what format the date in your string is in.

If you change your code to something like this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd yyyy"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:@"d:MMM"];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);

It should do what you want.

Benno
It Worked.So we have to specify what is our format is.Thanks for the help Benno.I was stuck in this for a while....Cheers.
deamonsarea