Reputation: 1447
What is the NSDateFormatter
string for the following date/Time : 02/22/2011 10:43:00 AM
Basically what I wanted was to read a string 02/22/2011 19:00:23 PM
, convert it to NSDate
and then print is appropriately. My previous attempt:
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSDate* dt3 = [dtFormatter dateFromString:@"02/22/2011 19:00:23 PM"];
NSString* strDate3 = [dtFormatter stringFromDate:dt3];
NSLog(@"Third = %@",strDate3);
Every time I tried that it would print NULL, it worked when I removed the trailing PM.
Finally, I added the following lines right after the 2nd line in the code above:
[dtFormatter setPMSymbol:@"PM"];
[dtFormatter setAMSymbol:@"AM"];
Upvotes: 4
Views: 8871
Reputation: 3960
Try it this will give you the required format..
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
// Conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:curDate]; //give what u want to give curDate
[formatter release];
// Conversion of NSDate to NSString
NSString *dateString = [formatter stringFromDate:datePicker.date]; //give date here
[formatter release];
This is the correct way if you want string from NSDate object...Now try it
Good Luck!
Upvotes: 2
Reputation: 8357
MM/dd/Y hh:mm:ss a
Fore more info see: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
Upvotes: 2