且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

dateFromString始终使用dateformatter返回null

更新时间:2022-04-10 07:35:15

你是在后台线程中这样做的吗?我没有在ui-thread中使用NSDateFormatter时遇到过奇怪的经历。无论如何,这是我使用的方法,应该适合你:

Are you doing this in a background thread? I had weird experiences with NSDateFormatter when not being used in the ui-thread. Anyway, here's the method I use, should work for you:

+ (NSDate*)parseDate:(NSString*)inStrDate format:(NSString*)inFormat {
    NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
    [dtFormatter setLocale:[NSLocale systemLocale]];
    [dtFormatter setDateFormat:inFormat];
    NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
    [dtFormatter release];
    return dateOutput;
}