iPhone:如何转换“yyyyMMddThhmmss”将字符串格式化为NSDate?
问题描述:
我有一个字符串,如 20121124T103000
,我想将其转换为 NSDate
。
I have a string like "20121124T103000
" and I want to convert this to NSDate
.
我尝试使用dateFormatter日期格式 yyyyMMddThhmmss
转换它,但它输出为 2001-01- 01 00:00:00 +0000
这是不正确的。
I tried converting it with dateFormatter date format "yyyyMMddThhmmss
" but it gives output as 2001-01-01 00:00:00 +0000
which is incorrect.
我们将此字符串转换为的方式是什么? NSDate
?
What is the way we can convert this string to NSDate
?
这是我的代码:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMddThhmmss"];
NSLog(@"%@",[dateFormat dateFromString:@"20121124T103000"]);
感谢任何帮助。
答
这样做,
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);