NSString *string = @"1990-04-15";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
return [dateFormatter dateFromString:string];
this always return null,it functions ok when "string" changed by any other date string,is this date(1990-04-15) so special?
xcode 5 , IOS 7, iphone app
the whole method and context:
in "Tools.m":
+(NSDate *)stringToDate:(NSString *)string{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
return [dateFormatter dateFromString:string];
}
and the method called:
NSDate *date = [Tools stringToDate:@"1990-04-15"];
NSLog(@"---- the date:%@",date);
Are you in China? If so, it is because there was a time shift to daylight saving time, so midnight April 15 1990 didn't exist - the time jumped directly to 1:00 AM. (You are not providing a time, and the default time is midnight. And you are not providing a time zone, so your own locale is assumed.)