简体   繁体   中英

Different result on converting date from string in iOS 13 and iOS 11.4

I'm having troubles on converting Date from string on iOS 11.4. Same func on iOS 11.4 is returning nil, but in iOS 13 returns correct value. I'm using Xcode 11.0, and this convert func:

+ (NSDate *)getDateFromStringDate:(NSString *)stringDate {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"];
    return [dateFormat dateFromString:stringDate];
}

NSISO8601DateFormatter knows more and is more flexible than any format string you can write. Rewrite your code as

+ (NSDate *)getDateFromStringDate:(NSString *)stringDate {
    NSISO8601DateFormatter * df = [NSISO8601DateFormatter new];
    return [df dateFromString:stringDate];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM