How to display the full month from an NSDate (such as "February")

There was me thinking I’d have to whip out NSDateComponents and NSCalendar – but my mind works too complicated at times.

All we need to do this is our good friend the NSDateFormatter and the Unicode Date Format MMMM – let’s check it out:

// create today's date
NSDate *myDate = [NSDate date];
// create a date formatter and set its format
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"MMMM";
NSLog(@"The current month is %@", [formatter stringFromDate:myDate]);

The MMMM tells the date formatter to display the full month, no matter how many letters it has. You can also use:

  • M for the month number (1 for January or 12 for December)
  • MM for the two-digit month number (01 for January)
  • MMM for the abbreviated month number (Jan for January)
  • MMMM for the full month number (such as January)
  • MMMMM for just the first letter (J for January… not sure who would need this though)

The Unicode Date Format can do amazing things with all aspects of times and dates – check out the complete compendium here:

Leave a Reply