Search code examples
objective-cnsdateformatterdate-formatting

Date formatter in Objective-C returns null


I don't understand why my dateFormatter returns null. The code is very simple, but I don't see any mistakes here. I have also tried to use setDateFormat, but without any result. What is the problem with the code?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
mikey.hireDate = [dateFormatter dateFromString:@"Aug 02, 2010"];

Solution

  • Set your date formatter's locale to EN_US_POSIX. That way, the device's region will not alter the formatter.

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