我有一个像“2012 12 12 12:12”这样的字符串,如何格式化为日期?
我试试这个但不工作
-(NSDate *)stringToDate:(NSString *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:timeZone];
[formatter setDateFormat : @"yyyy MM dd HH:mm"];
NSString *stringTime = date;
NSDate *dateTime = [formatter dateFromString:stringTime];
[formatter release];
return dateTime;
}
更新:当我改变格式化程序的位置时,它也不能通过:
NSLog(@"%@", stringToDate(@"2012 12 12 12:12"));
它输出“null”,但它适用于字符串格式,如“2012-12-12 12:12”