NSDateFormatter still parsing instead having incorrect format

后端 未结 3 653
北荒
北荒 2020-12-07 02:11

Having some problems parsing date. I have an array of supported formats and once I receive the date (string) from API, I try to parse it iterating through the formats until

3条回答
  •  死守一世寂寞
    2020-12-07 03:00

    This could be related to the fact that NSDateFormatter will anyways respects the users settings when using fixed formats

    Although in principle a format string specifies a fixed format, by default NSDateFormatter still takes the user’s preferences (including the locale setting) into account

    So may be the locale defined in your preference uses '/' for separator and satisfies the 'incorrect format'. Even if that is not the case, apple noted in several places that NSDateFormatter might not act consistently. So try setting a fixed locale as below and see if that helps

    NSLocale *locale = [[NSLocale alloc] 
        initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:locale];
    

    See these links for detail: apple tech note . Note directly related to separators, but that could be related.

提交回复
热议问题