Reputation: 32071
I have a date in NSString format with the following format:
2012-03-27 14:43:22 +0000 //this is a string
What I want is to somehow convert this to say: March 27
So I figured the way to do this was to convert this string to an NSDate, then format the new NSDate to my desired format, then back to string, but I'm getting null output:
NSString *date=[sectionInfo name];
NSLog(@"date:%@", date);
NSDateFormatter *k= [[NSDateFormatter alloc] init];
[k setDateFormat:@"yyyy-mm-DD hh:mm:ss a"];
NSDate *d=[k dateFromString:date];
NSLog(@"%@", d);
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"MMMM dd"];
NSString *dateString=[df stringFromDate:d];
NSLog(@"dateString:%@", dateString);
Output:
date:2012-03-28 14:43:22 +0000
(null)
dateString:(null)
What am I doing wrong?
Upvotes: 0
Views: 153
Reputation: 3262
I guess your format string should be "yyyy-MM-dd HH:mm:ss Z"
Also, try using getObjectValue:forString:range:error:
method instead of dateFromString:
and take a look at the error returned.
Upvotes: 1
Reputation: 6519
You need to have capital HH in your format, also change the a to z, like this:
[k setDateFormat:@"yyyy-mm-DD HH:mm:ss z"];
Upvotes: 1