简体   繁体   中英

Unable to convert date string to NSDate using NSDateFormatter

This is my iOS program fragment.I had some debugging, but have no choice.Why the log is returning null for createDate? Is the first time I use stackoverflow, so I don't know how to reply, I'm sorry.

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
//    Mon May 18 22:11:56 +0800 2015    _created_at

NSDate *createDate = [fmt dateFromString:_created_at];

NSLog(@"*****%@-----%@-----%@",createDate,[fmt stringFromDate:createDate],_created_at);
//NSLog:*****(null)-----(null)-----Mon May 18 22:50:28 +0800 2015

Why? Thanks.

Thank you for reply, this is me the code after the correction. But I don't know how to choose the right answer now, very sorry.

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    NSLocale* local =[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ;
    [fmt setLocale:local];
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";

//    Mon May 18 22:11:56 +0800 2015    _created_at

    NSDate *createDate = [fmt dateFromString:_created_at];

    NSLog(@"*****%@-----%@",createDate,_created_at);
    //NSLog:*****2015-05-18 16:12:21 +0000-----Tue May 19 00:12:21 +0800 2015

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