A bug? NSDateFormatter dateFromString Returns null

2019-02-19 14:53发布

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);

1条回答
来,给爷笑一个
2楼-- · 2019-02-19 15:33

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.)

查看更多
登录 后发表回答