0

I need to get all days of the week in their short mode, like this:

sun,mon,etc ..

I have tried this:

NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
[df setDateFormat:@"c"];

Which always gives the full names, how would you get the short ones?

luk2302
  • 55,258
  • 23
  • 97
  • 137
Curnelious
  • 1
  • 16
  • 76
  • 150

4 Answers4

7
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSArray * shortWeeks = [formatter shortWeekdaySymbols];
NSLog(@"%@",shortWeeks);

Log

   2015-07-14 17:25:31.470 NewOcTest[9016:286325] (
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
)

Also,there is also veryShortWeekdaySymbols in dateFormatter

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSArray * shortWeeks = [formatter veryShortWeekdaySymbols];
NSLog(@"%@",shortWeeks);

Log

2015-07-14 17:30:30.686 NewOcTest[9073:290513] (
S,
M,
T,
W,
T,
F,
S
)
Leo
  • 24,596
  • 11
  • 71
  • 92
2

One option is to use ccc

[df setDateFormat:@"ccc"];

Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the short day, four for the full name, five for the narrow name, or six for the short name.

See the unicode docs

Alternatively use eee or E - not sure what exactly the differences are.

Day of week - Use one through three letters for the short day, or four for the full name, five for the narrow name, or six for the short name.

or

Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the week, using one or two letters. For this example, Monday is the first day of the week.

luk2302
  • 55,258
  • 23
  • 97
  • 137
1

Day of the week in a short 3-char style like 'Mon', 'Tue', 'Wed', ...

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE"];
NSString *strDate = [formatter stringFromDate:date];

OR (for your code)

NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
[df setDateFormat:@"EEE"];
Kumar
  • 1,882
  • 2
  • 27
  • 44
1

use shortWeekdaySymbols, it gives an array of the names

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSDateFormatter * df = [[NSDateFormatter alloc] init];
        NSLog(@"%@ \n OR \n %@", df.weekdaySymbols, df. df.shortWeekdaySymbols);
    }
}

no need to use any formatting except if you are in fact not looking for the week but for a single date: How do I get the day of the week with Cocoa Touch?

Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135