NSDateFormatter returns null for specific dates

为君一笑 提交于 2019-12-04 05:23:34

I will answer my own question just to register in case of someone get the same problem:

I added to my date formatter [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; AND [df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];.

So, my new code is:

- (void) debugTest:(NSString *)dateStr{

    NSLog(@"String: %@", dateStr);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    [df setDateFormat:@"yyyy-MM-dd"];

    NSDate *dateObj = [df dateFromString: dateStr];

    NSLog(@"Date: %@", dateObj);

}

I think the problem was related to the device's configuration, setting the two properties fixed the problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!