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

- by

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:

http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.