5
votes

If I have an NSDate object, how can I format an NSString to output only the first 3 letters of the Day (like Mon, Tue, Wed, Thu, Fri, Sat, Sun) and the first 3 letters of the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)?

Currently I'm using this NSDateFormatter:

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        dateFormatter.dateFormat = @"MMMM dd yyyy";

        NSDate *date = ...
        NSString *dateString = [dateFormatter stringFromDate:date];

        NSLog(@"dateString: %@",dateString);
        // outputs August 09 2013
        // I would like the output to be Fri, August 9

Edit

I've also tried using the NSDateComponent but I'm wondering if there's some shortcut to getting the abbreviations...

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
1
Did you look at the date formatting specifiers? See unicode.org/reports/tr35/tr35-25.html#Date_Format_Patternsrmaddy
@rmaddy thanks sir. Didn't know that documentation existed. will refer to it from now on.Bob Yousuk
Look at the reference docs for NSDateFormatter. There is a link to "data formatters" which in turn has a link for "date formatters" which finally has a link to the page I referenced. It's real obvious ;)rmaddy

1 Answers

10
votes

As per the link kindly provided by rmaddy, @"EEE, MMM d"; is the solution