我在一个类中编写了这个函数:
- (NSDate *) convertDate : (NSString *) dateStr{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateFrmStr = [[NSDate alloc] init];
dateFrmStr = [dateFormatter dateFromString:dateStr];
return dateFrmStr;
}
我在另一个类中调用这个函数,如下所示:
NSString * dateStr1 =@"01-01-1977";
NSString * dateStr2 =@"22-12-1977";
NSString * dateStr3 =@"19-01-1978";
MyClass *data = [[MyClass alloc]init];
NSDate *dateObj1 = [data convertDate:dateStr1];
NSDate *dateObj2 = [data convertDate:dateStr2];
NSDate *dateObj3 = [data convertDate:dateStr3];
NSLog(@" >>> dateObj1 %@",dateObj1);
NSLog(@" >>> dateObj2 %@",dateObj2);
NSLog(@" >>> dateObj3 %@",dateObj3);
当我运行它时,唯一的第一个日期似乎被转换了,因为我得到的输出是:
>>> dateObj1 1977-01-01 00:00:00 +0000
没有错误,只是程序停止了。