Reputation: 953
here is code i am using for conversion
NSString * dateString = @"9/12/2011";
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
[serverDateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *serverDate = [serverDateFormatter dateFromString:dateString];
[serverDateFormatter release];
And output is 2011-09-11 19:00:00 +0000
But i just want to convert that nsstring into nsdate with the same format 9/12/2011
Upvotes: 1
Views: 446
Reputation: 3402
You can try below code because I run that code successfully.
NSString * dateString = @"9/12/2011";
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
[serverDateFormatter setDateFormat:@"d/MM/yyyy"];
NSDate *serverDate = [serverDateFormatter dateFromString:dateString];
NSLog(@"%@",serverDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"d/MM/yyyy"];
NSDate *st = (NSDate *)[dateFormat stringFromDate:serverDate];
NSLog(@"%@",st);
Its return 9/12/2011 so try it.
Upvotes: 0
Reputation: 17877
Set this properties to your NSDateFormatter
:
[serverDateFormatter setDateStyle:NSDateFormatterShortStyle];
[serverDateFormatter setTimeStyle:NSDateFormatterNoStyle];
Upvotes: 0
Reputation: 2281
Try this one.
NSString * dateString = @"9/12/2011";
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
[serverDateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *serverDate = [serverDateFormatter dateFromString:dateString];
NSLog(@"%@",serverDate);
NSDateFormatter *tempDate=[[NSDateFormatter alloc]init];
[tempDate setDateFormat:@"MM/dd/yyyy"];
NSDate *date=(NSDate *)[tempDate stringFromDate:serverDate];
NSLog(@"%@",date);
[tempDate release];
[serverDateFormatter release];
hope this will help you..
Upvotes: 1