Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread Display Modes
Old Sep 22, 2012, 08:09 PM   #1
macrumors 601
ArtOfWarfare's Avatar
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
NSDateFormatter printing the word "Month"?

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;
                    [string appendString:@" "];
                    [string appendString:[dayOfMonthDateFormatter stringFromDate:[obj objectForKey:@"pubDate"]]];
                    [string appendString:@" "];
            [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):
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.
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.

Last edited by ArtOfWarfare; Sep 23, 2012 at 12:05 AM.
ArtOfWarfare is offline   0 Reply With Quote
Old Sep 29, 2012, 02:39 AM   #2
macrumors 6502
Join Date: Nov 2010
Location: Sea of Tranquility
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.
CodeBreaker is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 11:06 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC