PDA

View Full Version : NSDateFormatter printing the word "Month"?




ArtOfWarfare
Sep 22, 2012, 08:09 PM
I'd like to make it so my app outputs NSStrings like this:

On Thursday, September Twentieth at 12:22

In my header I have:
NSDateFormatter* dayOfWeekDateFormatter;
NSDateFormatter* dayOfMonthDateFormatter;
NSDateFormatter* timeDateFormatter;

In my class's viewDidLoad I have:
dayOfWeekDateFormatter = [[NSDateFormatter alloc] init];
[dayOfWeekDateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"EEEE, MMMM" options:0 locale:[NSLocale currentLocale]]];
dayOfMonthDateFormatter = [[NSDateFormatter alloc] init];
[dayOfMonthDateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"d" options:0 locale:[NSLocale currentLocale]]];
timeDateFormatter = [[NSDateFormatter alloc] init];
timeDateFormatter.timeStyle = NSDateFormatterShortStyle;

In the code that builds the string:
NSMutableString* string = [[NSMutableString alloc] init];
[string appendString:@"On "];
[string appendString:[dayOfWeekDateFormatter stringFromDate:[obj objectForKey:@"pubDate"]]];
switch ([[dayOfMonthDateFormatter stringFromDate:[obj objectForKey:@"pubDate"]] intValue]) {
case 1: [string appendString:@" First at "]; break;
case 2: [string appendString:@" Second at "]; break;
case 3: [string appendString:@" Third at "]; break;
case 4: [string appendString:@" Fourth at "]; break;
case 5: [string appendString:@" Fifth at "]; break;
case 6: [string appendString:@" Sixth at "]; break;
case 7: [string appendString:@" Seventh at "]; break;
case 8: [string appendString:@" Eighth at "]; break;
case 9: [string appendString:@" Ninth at "]; break;
case 10: [string appendString:@" Tenth at "]; break;
case 11: [string appendString:@" Eleventh at "]; break;
case 12: [string appendString:@" Twelfth at "]; break;
case 13: [string appendString:@" Thirteenth at "]; break;
case 14: [string appendString:@" Fourteenth at "]; break;
case 15: [string appendString:@" Fifteenth at "]; break;
case 16: [string appendString:@" Sixteenth at "]; break;
case 17: [string appendString:@" Seventeenth at "]; break;
case 18: [string appendString:@" Eighteenth at "]; break;
case 19: [string appendString:@" Nineteenth at "]; break;
case 20: [string appendString:@" Twentieth at "]; break;
case 21: [string appendString:@" Twenty First at "]; break;
case 22: [string appendString:@" Twenty Second at "]; break;
case 23: [string appendString:@" Twenty Third at "]; break;
case 24: [string appendString:@" Twenty Fourth at "]; break;
case 25: [string appendString:@" Twenty Fifth at "]; break;
case 26: [string appendString:@" Twenty Sixth at "]; break;
case 27: [string appendString:@" Twenty Seventh at "]; break;
case 28: [string appendString:@" Twenty Eighth at "]; break;
case 29: [string appendString:@" Twenty Ninth at "]; break;
case 30: [string appendString:@" Thirtieth at "]; break;
case 31: [string appendString:@" Thirty First at "]; break;
default:
[string appendString:@" "];
[string appendString:[dayOfMonthDateFormatter stringFromDate:[obj objectForKey:@"pubDate"]]];
[string appendString:@" "];
break;
}
[script appendString:[speechTimeDateFormatter stringFromDate:[obj objectForKey:@"pubDate"]]];

Here's an example of output this is giving me though:
On Thursday (Month: September) Twentieth at 12:22

Where is "(Month: )" coming from? How can I remove it? This is occurring on device on my iPhone 4S running iOS 6. I don't know if it occurs on other devices/versions of iOS, yet. (Downloading and installing iOS 5 simulator now...)

Edit: Replacing the MMMM with MMM causes it to print:
On Sep Thursday Twentieth at 12:22

Why did it flip around the order of the day and month? (FTR, I still want the full month name, I was just seeing if other formatter strings behaved properly.)

I could do the same sort of thing I did to change the day of the month into an ordinal word... but it seems like that shouldn't be necessary and it should be pretty easy to get what I want...

2X Edit: Having just "MMMM" by itself without "EEEE, " preceding it causes it to print out the name of the month without the word month.

3X Edit: Having just "EEEE" by itself without the "MMMM" causes it to print out just the name of the day without the word month. But the following will all cause it to print the month as (Month: name of month):
"EEEE,MMMM"
"EEEE, MMMM"
"EEEEMMMM"
"EEEE MMMM"
"EEEE','MMMM"
"EEEE',' MMMM"

4X Edit: Flipping the EEEE and MMMM around changes absolutely nothing... it still prints the words in the exact same order.

5X Edit: The simulator running iOS 5.1 does the exact same thing, so this isn't a behavior that's new to iOS 6.

6X Edit: Simply including a "d" at the end of the string will cause it to drop the word "Month", but I want the number spelled out.

7X Edit: This doesn't explain why throwing in the "d" fixes it, but I just found the cause of why flipping the EEEE and MMMM changes nothing: "Returns a localized date format string representing the given date format components arranged appropriately for the specified locale.
Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale)."

Since in the US, the day of the week generally is said before the name of the month, it always orders them that way on my devices.



CodeBreaker
Sep 29, 2012, 02:39 AM
You can get rid of the system localisation, by using the formats of the Unicode Locale Data Markup Language, which are supported by NSDateFormatter (NSNumberFormatter).

Here's a handy document which will let you know how many 'E's, 'M's, 'h's to use.
http://www.unicode.org/reports/tr35/#Date_Format_Patterns