Bob Yousuk
Bob Yousuk

Reputation: 1175

Formatting an NSDate with Abbreviated Month/Day Names

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];

Upvotes: 5

Views: 4377

Answers (1)

Bob Yousuk
Bob Yousuk

Reputation: 1175

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

Upvotes: 10

Related Questions