-2

EDIT Start

Ok, obviously I didn't make myself clear.

  1. I need a "first-day-of-the-week"-independent bi-directional mapping of days of the week. So although the user might change his/her locale/region settings the stored day-numbers can still be used to identify the "correct" days.

  2. I know of NSDateFormatter's and NSCalendar's weekdaySymbols methods. But I couldn't find any documented direct relation between the indices of the returned array and the value found in NSDateComponents' weekday property in the API/Apple docs. Yes, there are a lot of posts (here and elsewhere) doing like [NSCalendar weekdaySymbols][dateComps.weekday - 1] (or even [NSCalendar weekdaySymbols][dateComps.weekday]). But, apart from the fact that I had problems mapping the day-IDs back to weekday of an actual NSDateComponents (see second code snippet from original post), I prefer being sure before handing over the project to a/the client.

  3. The problem I encounter with the first code snippet (posted in my original post) is that NSCalendar's dateFromComponents: doesn't honor the weekday property of the passed NSDateComponents instance.

EDIT End

Hi I'm trying to build a day-number (like 1 for sunday) to day-name (like "sunday") mapping. So that I can present the list of days (Monday to Sunday or Sunday to Monday or whatever follows the user's calendar-preferences) to the user. The mapping is needed, so that I can store the number(s) of the days the user selects in a localization independent way.

I know of NSDateFormatter's or NSCalendar's weekdaySymbols. But as I understand that these are just lists, without a relation to the user's preferences (in terms of first-day-of-week) or dayOfWeek (as might be returned by a NSDateComponents instance), I don't see how I might use those.

I tried several approaches....but something always breaks the thing up. The following version for example starts fine with Monday as the first value assigned to dayName. But further iteration don't seem to have affect on the date. So I get a list of 7 Monday strings.

    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:7];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];

    NSDate *date = nil;
    NSString *dayName = nil;;

    NSDictionary *idToNameDict = nil;
    NSInteger weekday = [cal firstWeekday];

    for (NSInteger i = 0; i < 7; i++) {

        dateComps.weekday = weekday;
        date = [cal dateFromComponents:dateComps];

        dayName = [dateFormatter stringFromDate:date];

        idToNameDict = [NSDictionary dictionaryWithObject:dayName forKey:@( dateComps.weekday )];

        [tempArray addObject:idToNameDict];

        weekday++;
        if (weekday > 7) {
            weekday = 1;
        }
    }

In short, I have the following requirements:

  • User-calendar dependent presentation (start/end of week) of days of the week
  • Save the days the user selects in a non-localized way (as kind of day-ID)
  • Check what ever date if it "matches" one of the days selected by the user before.

I surly have missed something. But I can't seem to find the missing part. Before I used just the weekdaySymbols and added 1 to the element-index to map the day-ID to the name. But then the following code hat e.g. weekday with a value of 2 for a Tuesday, whereas when building up the list Tuesday was associated with 3.

NSDate *givenDate = ...; // from somewhere
NSInteger userSelectedDay = ...; // from data store
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:givenDate];
if (comps.weekday == userSelectedDay) {
    // highlight or whatever    
}

It might has todo with time-zone "stuff", but as I never set (or remove) the timezone, I can't see how/where.

dergab
  • 965
  • 7
  • 16
  • repeated question http://stackoverflow.com/questions/7330420/how-do-i-get-the-name-of-a-day-of-the-week-in-the-users-locale – Shoaib Aug 31 '15 at 19:38
  • and also http://stackoverflow.com/questions/18316614/how-to-get-the-day-of-week-from-a-given-number – Shoaib Aug 31 '15 at 19:39
  • 1
    It's completely unclear to me what you're asking. `weekdaySymbols` is already, and always, a mapping from weekday number to name. It doesn't change with the calendar's `firstWeekday`. – jscs Aug 31 '15 at 19:58
  • @Shoaib: Not a duplicate as far as I can see. As I stated, that I know about `weekdaySymbols` method(s). I've updated my post so that it might get clearer where my problems are. – dergab Sep 01 '15 at 07:37
  • @JoshCaswell: Is `weedaySymbols` _already, and always, a mapping from weekday number to name_? The docs provided by apple, at least as far as I found them, doesn't state this. – dergab Sep 01 '15 at 07:40
  • @dergab you mean to say id 1 is not equal to Sunday for all locales? what is the device locale, you are testing on? – Shoaib Sep 01 '15 at 09:14
  • @Shoaib, no I haven't encountered a situation, where day-ID `1` is not Sunday. But on the other hand I couldn't find Apple stating, that **A)** the (e.g.) Sunday symbol of `weekdaySymbols` is and will be always on index `0`, and **B)** Sunday (or how it might be named in other languages) will always be reflected as `weekday == 1` of `NSDateComponents`. It seems to me, that I will have to implement the application logic _assuming_ this (-> A) and B)). But I would have preferred to have a clear statement from the API/Apple docs on this. ;) – dergab Sep 01 '15 at 09:35
  • 1
    Source: [Unicode Technical Standard #35](http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns) <!ATTLIST day type ( sun | mon | tue | wed | thu | fri | sat ) #REQUIRED > – zaph Sep 01 '15 at 12:04
  • 1
    WRT the updated question point: 3: From the Apple docs `NSCalendar dateFromComponents`: "Unnecessary components are ignored (for example, Day takes precedence over Weekday and Weekday ordinals)." A date can't be created given a weekday, there are several date possibilities and day defaults to 1. – zaph Sep 01 '15 at 12:19
  • Thanks, @zaph. This would explain the 7-Mondays problem. – dergab Sep 01 '15 at 17:26

1 Answers1

1

Notes:

The timezone used is the users when the data is created.
Sunday == 1

Weekday number from a date:

NSDate *date = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:date];
NSInteger weekdayNumber = dateComponents.weekday;

Day name from a date:

NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"EEEE";
NSString *dayName = [dateFormatter stringFromDate:date];

Week name from a number:

NSArray *weekDayNames = [[NSDateFormatter new] weekdaySymbols];
NSString *weekDayName = weekDayNames[2];
zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thanks @zaph for the examples. But as I stated, I know about the `weekdaySymbols`. And my code snippets as well showed that I know about `NSDateFormatter`, `NSCalendar and `NSDateComponents` classes and their usage as such. I've updated my post to be (hopefully) clearer regarding my problem(s). – dergab Sep 01 '15 at 07:33