I just referred to the Q & A at iOS reference for NSDateFormatter.
Link: http://developer.apple.com/library/ios/#qa/qa2010/qa1480.html
Here is my problem I get a string from a webservice which is of this format:
"dd-mm-yyyy xx:xx:xx AM"
My actual purpose is just to use the date and not the time at all. But I just use the same format in the NSDateFormatter and I was able to get the answers properly for all date related problems except on one mobile which is iOS 4.1.
Please let me know what is the most optimal solution for this problem. I think, I should just use date and that will 开发者_运维知识库solve my problem or any other suggestions for this problem?
Your question wasn't really helpful in terms of the result you get form iOS 4.1. Anyways...
Try this...
// convert date
NSString *webStr = [NSString stringWithFormat:@"28-10-2010 04:44:22 AM"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss a"];
NSDate* date = [formatter dateFromString:webStr];
// set up the new date format
[formatter setDateFormat:@"dd-MM-yyyy"];
NSString *myDate = [formatter stringFromDate:date];
[formatter release];
精彩评论