NSDateFormatter returns nil for @ "dd-MM-yy" in iOS 3.0

I have this piece of code:

NSDate *date =nil; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setDateFormat:@"dd-MM-yy"]; date = [dateFormatter dateFromString:inString]; [dateFormatter release]; 

It works great, as expected in iOS 4.0. But the same code is not in 3.0.

The string I get is similar to "12-Nov-10" and it is contained in the inString pointer.

The date formatter returns nil if the native OS is 3.0 or 3.1. For some reason I need to stick to the same date format. Has anyone else encountered this problem? Any suggestions to fix this problem?

Thanks,

Rajah

Edit: The correct code, after the following sentences indicated by Kharkonyan, and Q & A questions:

 NSDate *date =nil; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setDateFormat:@"dd-MMM-yy"]; NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; [dateFormatter setLocale:enUSPOSIXLocale]; [enUSPOSIXLocale release]; date = [dateFormatter dateFromString:inString]; [dateFormatter release]; 
+2
cocoa-touch ios4 nsdate nsdateformatter
source share
2 answers

If you are working with user-visible dates, you should avoid setting the date format string, because it is very difficult to predict what your format string will look like in all possible user configurations. Rather, you should try to limit yourself to date and time style settings (via - [NSDateFormatter setDateStyle:] and - [NSDateFormatter setTimeStyle:]).

On the other hand, if you are working with fixed-format dates, you must first set the date formatting locale to something suitable for your fixed-format. In most cases, the best choice is "en_US_POSIX", a language that is specifically designed to produce results in English, regardless of user and system preferences. "en_US_POSIX" is also time-invariant (if the US at some point in the future changes the format of dates, "en_US" will change to reflect the new behavior, but "en_US_POSIX" will not) between machines ("en_US_POSIX" also works on iPhone OS, as on Mac OS X, as well as on other platforms).

Once you have set "en_US_POSIX" as the language format for date formatting, you can set the date format string, and the date format will behave consistently for all users.

The above information and more can be found at Apple Technical Q & A QA1480

Here is a snippet of code from my application that implements the above recommendation:

 static NSDateFormatter* dateFormatter = nil; if (!dateFormatter) { dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; assert(enUSPOSIXLocale != nil); [dateFormatter setLocale:enUSPOSIXLocale]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000"; } 
+7
source share

An unusually useful page no longer exists, so just put its contents here for archival purposes:

 a: AM/PM A: 0~86399999 (Millisecond of Day) c/cc: 1~7 (Day of Week) ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday d: 1~31 (0 padded Day of Month) D: 1~366 (0 padded Day of Year) e: 1~7 (0 padded Day of Week) E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday F: 1~5 (0 padded Week of Month, first day of week = Monday) g: Julian Day Number (number of days since 4713 BC January 1) G~GGG: BC/AD (Era Designator Abbreviated) GGGG: Before Christ/Anno Domini h: 1~12 (0 padded Hour (12hr)) H: 0~23 (0 padded Hour (24hr)) k: 1~24 (0 padded Hour (24hr) K: 0~11 (0 padded Hour (12hr)) L/LL: 1~12 (0 padded Month) LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec LLLL: January/February/March/April/May/June/July/August/September/October/November/December m: 0~59 (0 padded Minute) M/MM: 1~12 (0 padded Month) MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec MMMM: January/February/March/April/May/June/July/August/September/October/November/December q/qq: 1~4 (0 padded Quarter) qqq: Q1/Q2/Q3/Q4 qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter Q/QQ: 1~4 (0 padded Quarter) QQQ: Q1/Q2/Q3/Q4 QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter s: 0~59 (0 padded Second) S: (rounded Sub-Second) u: (0 padded Year) v~vvv: (General GMT Timezone Abbreviation) vvvv: (General GMT Timezone Name) w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year) W: 1~5 (0 padded Week of Month, 1st day of week = Sunday) y/yyyy: (Full Year) yy/yyy: (2 Digits Year) Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year) YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year) z~zzz: (Specific GMT Timezone Abbreviation) zzzz: (Specific GMT Timezone Name) Z: +0000 (RFC 822 Timezone) 
+9
source share

All Articles