I have data coming to my app which is a date stored in a string as follows:
“Tuesday 31st January 2011 10:15am”.
I have tried using dateFromString, and then NSDateFormatter to change it into another format (ideally I would like it displayed as hh:mm (24 hour) dd/mm/yyyy). I cannot find a way to remove the weekday name, and so whenever I try to print the result it comes out as null.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//is EEEE is for weekdays?
[df setDateFormat:@"EEEE-dd-MM-yyyy hh:mm a"];
NSDate *myDate = [df dateFromString:[[_feeds[_currentTableView] objectAtIndex:row] objectForKey:@"created"]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:myDate];
NSString *theTime = [timeFormat stringFromDate:myDate];
NSLog(@"\n"
"theDate: |%@| \n"
"theTime: |%@| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];
Can anyone help out? Thanks
You should isolate the problem to either parsing or formatting. My guess is that it’s the parsing. Here’s your sample string:
And your format:
They don’t match:
Leaving aside the “st” part, I suspect you’d want a format of:
… but I don’t know how you’ll deal with the “st” (or “th” or “nd”) elegantly, other than by just messing with the string explicitly.