views:

28

answers:

2

hi to all I am Struggling to convert NSString to NSDate with the Help of formatter but its going to be null Can any one help please here is my code...

 NSString *dateString=[NSString stringWithFormat:@"10/26/2010/09:56:56 PM"];
 NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormat setDateFormat:@"mm/dd/yyyy HH:mm:ss a"];
 NSDate *localDateTime =[dateFormat dateFromString:dateString];
 NSLog(@"client date is %@",localDateTime);

Thx in Advance

+1  A: 

Could be because your dateString and format does not match.

10/26/2010/09:56:56 PM

mm/dd/yyyy HH:mm:ss a

Do you see the difference? :) They actually have to match. In the format you have a space between the year and hours and in the actual date there is a slash. Month you match by 'mm' which is actually minutes and should be 'MM'. Change to:

10/26/2010 09:56:56 PM

MM/dd/yyyy HH:mm:ss a

There might be more problems so I suggest you actually look at the guide to get it right.

willcodejavaforfood
A: 
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

NSDate *formatterDate = [dateFormatter dateFromString:@"1999-07-11 at 10:30"];
[dateFormatter release];

Try this out.

sherry