IPhone: How to convert the format string "yyyyMMddThhmsms" to NSDate?

I have a string like " 20121124T103000 " and I want to convert it to NSDate .

I tried converting it to the dateFormatter date format " yyyyMMddThhmmss ", but it gives a result like 2001-01-01 00:00:00 +0000 , which is incorrect.

How can we convert this string to NSDate ?

Here is my code:

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyyMMddThhmmss"]; NSLog(@"%@",[dateFormat dateFromString:@"20121124T103000"]); 

Any help is appreciated.

+1
source share
3 answers

do so

 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); 
+1
source

Your original code is pretty close; instead:

 @"yyyyMMddThhmmss" 

You should use:

 @"yyyyMMdd'T'hhmmss" 

The only difference is a pair of single quotes around “T” in the string — you just need to tell that “T” is part of the formatting, not the actual date.

+4
source

Try using 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]); 
+1
source

All Articles