I have a string like \"20121124T103000
\" and I want to convert this to NSDate
.
I tried converting it with dateFormatter date format \"
Your original code is quite close; instead of:
@"yyyyMMddThhmmss"
You should be using:
@"yyyyMMdd'T'hhmmss"
The only difference is the pair of single quotes around the 'T' in the string- you just need to let the app know that 'T' is a part of the formatting, not the actual date.
do like this,
NSString *dateStr = @"20121124T103000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd'T'hhmmss"];
NSDate *d = [dateFormat dateFromString:dateStr];
NSString *st = [dateFormat stringFromDate:d];
NSLog(@"%@",st);
Try the following Code.
NSString *dateStr = @"20121124T103000";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:@"yyyyMMdd'T'hhmmss"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd'T'hhmmss"];
NSString *st = [dateFormat stringFromDate:d];
NSLog(@"%@",st]);